Data Structures & AlgorithmsVery hardICPC regional · sieve application
Problem: How many integers in [1, 100] are the product of exactly two distinct primes? Run the code.
Problem: How many integers in [1, 100] are the product of exactly two distinct primes? Run the code.
def primes_upto(n):
s = [True] * (n + 1)
s[0] = s[1] = False
for i in range(2, int(n ** 0.5) + 1):
if s[i]:
for j in range(i * i, n + 1, i):
s[j] = False
return [i for i in range(n + 1) if s[i]]
P = primes_upto(100)
print(sum(1 for x in range(1, 101) for i, p in enumerate(P) for q in P[i + 1:] if p * q == x))What is printed?- A. 34
- B. 25
- C. 40
- D. 30
Correct answer: D. 30
Products of two distinct primes up to 100 are the 30 squarefree semiprimes; squares such as 4, 9, 25 and 49 are excluded.
More Coding Interview Problems questions
- Problem: What is the minimum length of a contiguous subarray of [1, 2, 3, 4, 5, 6, 7, 8] with sum at least 15? Run the two-pointer code…
- Problem: What is the integer floor of the square root of 10^12? Use binary search. python lo, hi = 0, 10 12 while lo < hi: mid = (lo + hi…
- Problem: Split [7, 2, 5, 10, 8] into 2 contiguous parts to minimise the largest part sum. What is the answer? Run the brute-force code…
- Problem: How many contiguous subarrays of [4, 1, -1, 2, 3] have a sum divisible by 4? Run the prefix-count code. python from collections…
- Problem: How many distinct substrings of length 2 does 'abcabc' contain? Run the code. python s = 'abcabc' print(len({s[i:i + 2] for i in…
- Problem: What is the length of the longest valid (well-formed) parenthesis substring of ')()())'? Run the stack code. python s = ')()())'…