ProgrammingVery hardTCS NQT Advanced · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function isPrime(Integer n)
    if (n < 2) return false
    for (each d from 2 to n - 1)
        if (d * d > n) break
        if (n mod d == 0) return false
    end for
    return true
End Function
Integer c = 0
for (each p from 2 to 50)
    if (isPrime(p) AND isPrime(100 - p))
        c = c + 1
    end if
end for
Print c
  1. A. 5
  2. B. 12
  3. C. 6
  4. D. 7

Correct answer: C. 6

With p ≤ 50 each split of 100 into two primes p + q (p ≤ q) is counted once: 3 + 97, 11 + 89, 17 + 83, 29 + 71, 41 + 59, 47 + 53. c = 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic