ProgrammingVery hardTCS NQT · Accenture

The procedure below uses the sieve of Eratosthenes on the range 2 to 50, marking composites starting from ii. The inner marking statement executes once for each multiple it marks. How many times is the marking statement executed in total?

The procedure below uses the sieve of Eratosthenes on the range 2 to 50, marking composites starting from i*i. The inner marking statement executes once for each multiple it marks. How many times is the marking statement executed in total?
for i = 2 to 50
    if i is not marked then
        for j = i*i to 50 step i
            mark j; count = count + 1
        end for
    end if
end for
Print count
  1. A. 45
  2. B. 62
  3. C. 77
  4. D. 49

Correct answer: A. 45

Marking starts at i*i, so the statement runs 45 times in total. Starting at 2i gives 62 runs, starting at i also marks each prime itself and gives 77, and counting every number instead of the primes gives 49.

More Pseudocode and Code Tracing questions

Exams that ask this topic