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])- A. 25 30
- B. 25 29
- C. 24 29
- 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
- What is printed? python def merge(a, b): i = j = 0 out = [] while i < len(a) and j < len(b): if a[i] <= b[j]: out.append(a[i]) i += 1…
- What is printed? python from collections import deque def maxdist(g): R, C = len(g), len(g[0]) d = [[-1] C for in range(R)] q = deque()…
- What is printed? python from collections import Counter c = Counter('mississippi') print(c.mostcommon(2))
- What is printed? python def maxprod(a): best = curmax = curmin = a[0] for x in a[1:]: cands = (x, curmax x, curmin x) curmax, curmin =…
- What is printed? python def jumps(a): j = cur = far = 0 for i in range(len(a) - 1): far = max(far, i + a[i]) if i == cur: j += 1 cur = far…
- What is printed? python def profit(p): lo, best = float('inf'), 0 for x in p: lo = min(lo, x) best = max(best, x - lo) return best…