ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
def primes(n):
    s = [True] * (n + 1)
    s[0:2] = [False, False]
    for i in range(2, int(n ** 0.5) + 1):
        if s[i]:
            s[i * i::i] = [False] * len(s[i * i::i])
    return [i for i, v in enumerate(s) if v]
print(len(primes(100)), primes(30)[-1])
  1. A. 25 30
  2. B. 25 29
  3. C. 24 29
  4. D. 26 31

Correct answer: B. 25 29

There are 25 primes up to 100, and the largest prime not above 30 is 29. The sieve starts marking multiples of each prime at its square.

More Python Programming questions

Exams that ask this topic