Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional
How many integers n with 1 ≤ n ≤ 10000 have exactly four positive divisors? Count them with a divisor-count sieve.
How many integers n with 1 ≤ n ≤ 10000 have exactly four positive divisors? Count them with a divisor-count sieve.
N=10000
cnt=[0]*(N+1)
for d in range(1,N+1):
for m in range(d,N+1,d): cnt[m]+=1
print(sum(1 for n in range(1,N+1) if cnt[n]==4))What is printed?- A. 25
- B. 2608
- C. 3863
- D. 1353
Correct answer: B. 2608
Numbers with exactly four divisors are p·q (distinct primes) or p³. Counting exactly-three-divisor numbers gives squares of primes, cumulative counts include numbers with fewer divisors, and stopping the range at 5000 drops half of the valid numbers.
More Coding Interview Problems questions
- Array a = [3, 8, 2, 9, 5, 7, 6]. Count the index triples i < j < k such that the three values, sorted, are equally spaced (an arithmetic…
- Count the binary strings of length 10 that contain no two adjacent 1s and also contain a run of at least three consecutive 0s. Brute-force…
- Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code. python print(sum(1 for a in range(1,21) for…
- Floyd–Warshall is run on the 4-vertex directed weighted graph with matrix rows [0,3,∞,7], [8,0,2,∞], [5,∞,0,1], [2,∞,∞,0]. Sum the final…
- A rod of length 8 can be cut into integer pieces, and a piece of length L sells for p[L] with p = [1, 5, 8, 9, 10, 17, 17, 20] for L =…
- Array b = [1, 11, 2, 10, 4, 5, 2, 1]. Find the length of its longest bitonic subsequence: a subsequence that strictly increases and then…