ProgrammingHardTCS NQT · Infosys SP

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Boolean comp[0..30] = all false
Integer s = 0
for (each i from 2 to 30)
    if (not comp[i])
        s = s + i
        for (each j from i * i to 30 step i)
            comp[j] = true
        end for
    end if
end for
Print s
  1. A. 100
  2. B. 160
  3. C. 128
  4. D. 129

Correct answer: D. 129

The sieve leaves exactly the primes up to 30 unmarked: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, whose sum is 129.

More Pseudocode and Code Tracing questions

Exams that ask this topic