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?
- A. O(log n) divisions in the worst case
- B. O(n) divisions, because each division costs O(n)
- C. O(n log n) divisions in the worst case
- 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
- What is printed? python from string import Template print(Template('a-b').substitute(a=1, b=2), Template('ac').safesubstitute(a='x')…
- What is printed? python try: print((300).tobytes(1, 'big')) except OverflowError: print('overflow') print((255).tobytes(1, 'big')…
- What is printed? python print(bool('0'), bool(0.0), bool(-0.0), bool(float('nan')), bool(' '))
- What is printed? python print(True & 2, True | 2, True ^ True, ~True)
- What is printed? python print(-263 // -1 == 263, abs(-263) == 263, (263) (263) > 2127, 263 263 == 2126)
- What is printed? python print(int(1e22) == 1022, int(1e23) == 1023, float(1022) == 1e22)