Data Structures & AlgorithmsHardICPC regional · number theory

Problem: Which complexity does the sieve of Eratosthenes have for finding all primes up to n?

  1. A. O(n)
  2. B. O(n log n)
  3. C. O(n sqrt n)
  4. D. O(n log log n)

Correct answer: D. O(n log log n)

Each prime p marks about n/p multiples, and summing 1/p over primes up to n gives log log n, so the total is O(n log log n).

More Coding Interview Problems questions

Exams that ask this topic