ProgrammingHard
Trial division factorises a number up to 10^12 with for (i = 2; i * i <= n; i++) while (n % i == 0) n /= i;. About how many outer iterations run in the worst case?
- A. About 10^12
- B. About 10^3
- C. About 10^6
- D. About 40
Correct answer: C. About 10^6
The loop stops at sqrt(n) = 10^6 when n is prime. Testing up to sqrt(n) is what makes factoring feasible.
More C Programming questions
- A 2 x 2 matrix is raised to the power 10^18 by repeated squaring. How many squaring steps are needed?
- Which statement about the nested loop for (i = 0; i < n; i++) for (j = 1; j < n; j = 2) is correct?
- Which operation is O(1) in a C array, but O(n) in a singly linked list when the head is given?
- A program reads a number n and then loops for (i = 0; i < n; i++) with n = 10^9 and a constant body. Roughly how long does it take in C…
- Which operation makes a sorted array of n elements support lookup in O(log n) and insertion in O(n)?
- For a char buffer of length 10^6 that is processed once, which loop is correct and fast enough?