ProgrammingHardTCS NQT Advanced · Infosys SP

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Boolean prime[2..50]                     // prime[i] is true exactly when i is prime
Integer c = 0
for (each n from 2 to 50)
    for (each p from 2 to n - 1)
        if (prime[p] AND n mod p == 0 AND prime[n / p] AND p < n / p)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 15
  2. B. 13
  3. C. 17
  4. D. 10

Correct answer: B. 13

Each n = p × q with primes p < q is counted once. Up to 50: 6, 10, 14, 22, 26, 34, 38, 46 (with 2), 15, 21, 33, 39 (with 3), 35 (5 × 7): 13 numbers. Squares like 4, 9, 25, 49 are excluded by p < n / p.

More Pseudocode and Code Tracing questions

Exams that ask this topic