ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
def spf(n):
    s = list(range(n + 1))
    for i in range(2, int(n ** 0.5) + 1):
        if s[i] == i:
            for j in range(i * i, n + 1, i):
                if s[j] == j:
                    s[j] = i
    return s
s = spf(30)
out = []
x = 28
while x > 1:
    out.append(s[x])
    x //= s[x]
print(out, s[29])
  1. A. [2, 2, 7] 29
  2. B. [2, 7] 29
  3. C. [2, 2, 7] 2
  4. D. [2, 2, 7, 7] 29

Correct answer: A. [2, 2, 7] 29

The smallest-prime-factor table lets each step divide out one prime. 28 gives 2, then 14 gives 2, then 7, so the list is [2, 2, 7]. s[29] is 29 because 29 is prime.

More Python Programming questions

Exams that ask this topic