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))- A. 4 25
- B. 5 25
- C. 4 24
- 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
- What is printed? python a = [1, 2, 3] b = a b += [4] c = a + [5] a = a 2 print(b, c, a)
- What is printed? python def stepstocycle(f, s): seen = {} i = 0 while s not in seen: seen[s] = i s = f[s] i += 1 return i, i - seen[s]…
- What is printed? python from collections import Counter def divpairs(a, k): c = Counter(x % k for x in a) total = 0 for r in c: s = (k…
- What is printed? python def isrot(a, b): return len(a) == len(b) and b in a + a print(isrot('abcde', 'cdeab'), isrot('abc', 'acb')…
- What is printed? python def egcd(a, b): if b == 0: return a, 1, 0 g, x, y = egcd(b, a % b) return g, y, x - (a // b) y g, x, y = egcd(240…
- What is printed? python def canjump(a): far = 0 for i, x in enumerate(a): if i > far: return False far = max(far, i + x) return True…