Data Structures & AlgorithmsHardCodeforces Div2 C/D · Amazon SDE OA

How many primes p satisfy p ≤ 1,000,000 and p ≡ 3 (mod 4)?

How many primes p satisfy p ≤ 1,000,000 and p ≡ 3 (mod 4)?
N=10**6
s=bytearray([1])*(N+1); s[0]=0; s[1]=0
for i in range(2,int(N**0.5)+1):
    if s[i]: s[i*i::i]=bytearray(len(s[i*i::i]))
print(sum(1 for p in range(N+1) if s[p] and p%4==3))
What is printed?
  1. A. 4808
  2. B. 39175
  3. C. 39322
  4. D. 19653

Correct answer: C. 39322

A sieve marks composites, then the count is taken over primes in the residue class 3 mod 4. Counting class 1 mod 4 is a different count, shrinking the bound changes the answer, and mixing up mod 4 with mod 8 misclassifies many primes.

More Coding Interview Problems questions

Exams that ask this topic