ProgrammingHardTCS NQT · Accenture

Count the numbers n with 1 <= n <= 100 such that both n and n + 2 are prime (twin primes, lower member n). The procedure below checks primality by trial division and prints the count. What is printed?

Count the numbers n with 1 <= n <= 100 such that both n and n + 2 are prime (twin primes, lower member n). The procedure below checks primality by trial division and prints the count. What is printed?
count = 0
for n = 1 to 100
    if n is prime and n + 2 is prime then count = count + 1
end for
Print count
  1. A. 1
  2. B. 8
  3. C. 25
  4. D. 50

Correct answer: B. 8

The lower members are 3, 5, 11, 17, 29, 41, 59 and 71, which is 8. Checking n and n + 1 gives only 2 and 3 which is 1, counting the primes up to 100 gives 25, and counting odd pairs without a primality test gives 50.

More Pseudocode and Code Tracing questions

Exams that ask this topic