Data Structures & AlgorithmsModerateTCS NQT · number theory

Problem: What is the complexity of checking whether a number n is prime by trial division up to sqrt(n)?

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

Correct answer: C. O(sqrt n)

Trial division up to sqrt(n) takes about sqrt(n) divisions, which is fast for n up to 10^12.

More Coding Interview Problems questions

Exams that ask this topic