1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnac
ID: 3590780 • Letter: 1
Question
1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is (n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, N, N1.5, N2, N log N, N log logN, N log2 N, N log (N2), 2/N, 2V, 2N/2, 37, N2 log N, N3. Indicate which functions grow at the same rate. 3. (10 points) Compute the running time T(n) of the program fragment below and provide an analysis of the running time (Big-Oh notation will do). For convenience, assume that operations inside for loops take constant time, i.e. (1) sum = 0 for( i-0; iExplanation / Answer
If you post ore than 1 question, then as per chegg guidelines I just have to answer one question.
1.
function Fibonnaci(n):
{
// first term of series
if n == 0
return 1
if n == 1
return 2
a = 1
b = 2
for i <- 2 to n
{
c = a + b
a = b
b = c
}
return b
}
The above function computes n th fibonnaci number in
O(n) Time Complexity
and O(1) Space Complexity
It is so because the above function checks the two conditions for n == 0 and n == 1 in O(1) time.
The for loop runs for n - 2 times.
Hence, Time Complexity = O(1) + O(1) + O(n - 2)
= O(n - 2) (as O(1) is negligible)
= O(n) (as (n - 2) = n if n is very large)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.