ProgrammingVery hard
A recursive naive Fibonacci fib(n) makes calls(n) = calls(n-1) + calls(n-2) + 1 with calls(0) = calls(1) = 1. Roughly how many calls does fib(40) make?
- A. About 3.3 x 10^8
- B. About 4 x 10^7
- C. About 10^6
- D. About 40
Correct answer: A. About 3.3 x 10^8
calls(40) = 331160281. Memoising or iterating reduces the work to O(n), which is why the naive form is too slow.
More C Programming questions
- A loop computes the number of decimal digits of n = 10^18 with while (n) { n /= 10; cnt++; }. What value does cnt reach?
- 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…
- 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…