GATE Computer ScienceVery hardGATE CS (PYQ pattern)

for (i = 2; i <= 100; i++) if (!comp[i]) for (j = i * i; j <= 100; j += i) { comp[j] = 1; cnt++; } With comp[] initially all 0, what is the final value of cnt?

  1. A. 89
  2. B. 131
  3. C. 25
  4. D. 104

Correct answer: D. 104

Only primes i with i*i <= 100 (2, 3, 5, 7) enter the inner loop, which runs floor((100 - i*i)/i) + 1 times: 49 + 31 + 16 + 8 = 104 (note that composites are marked repeatedly, so cnt exceeds 75).

More Programming and Data Structures (GATE) questions

Exams that ask this topic