ProgrammingModerateProduct-company MCQ pattern

What is the time complexity of checking whether a number n is prime by trial division up to its square root?

  1. A. O(log n) divisions in the worst case
  2. B. O(n) divisions, because each division costs O(n)
  3. C. O(n log n) divisions in the worst case
  4. D. O(sqrt(n)) divisions in the worst case

Correct answer: D. O(sqrt(n)) divisions in the worst case

Testing divisors up to sqrt(n) takes about sqrt(n) steps when n is prime, which is the worst case. Each division itself is cheap for machine-size ints, so the count of divisions dominates.

More Python Programming questions

Exams that ask this topic