ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
def sieve(n):
    s = bytearray([1]) * (n + 1)
    s[0:2] = bytes(2)
    for i in range(2, int(n ** 0.5) + 1):
        if s[i]:
            s[i * i::i] = bytearray(len(s[i * i::i]))
    return sum(s)
print(sieve(10), sieve(100))
  1. A. 4 25
  2. B. 5 25
  3. C. 4 24
  4. D. 4 26

Correct answer: A. 4 25

The bytearray holds 1 for each candidate, and slice assignment crosses out multiples of each prime. The sum counts the survivors: 4 primes up to 10 and 25 up to 100.

More Python Programming questions

Exams that ask this topic