Python programming question. Problem 4: Better and worse Grading criteria: corre
ID: 3876609 • Letter: P
Question
Python programming question.
Problem 4: Better and worse Grading criteria: correctness (of the code) and thoroughness (of the explanation) 4a. Read about recursion in Python. Then write two different functions that, on an input N, return the Nth Fibonacci number: one using recursion, and one not. In : def fibl(N): # your code goes here In def fib2(N): # your code goes here Fn -1+F-2; use TeX symbols to format this correctly. Then write some code that 4b. State an identity of Fibonacci numbers other than the defining recurrence tests this identity using your two functions. In Your identity: (fill in here) In [ ]: # Your test code goes here 4c. Give as detailed an explanation of possible of why your non-recursive function is "better' than the recursive one. You may wish to do some online research for this, in which case please cite your sources. InExplanation / Answer
def feb1(N):
#using recursion
if (N<0):
return -1;#
if (N==0):
return 0;
if (N==1):
return 1;
else :
return feb1(N-1)+feb1(N-2);
def feb2(N):
#using loops
if (N==0):
return 0;
if (N==1):
return 1;
else:
f=0
s=1
n=0;
i=0;
while(i<N-1):
n=f+s
f=s
s=n
i=i+1
return n;
def main():
print feb1(6);
print feb2(6);
main();
4.c)
Non-recursive program is more better than recursive code. because recursive code executes in stacks need more space for execution. and a non-recursive code need not all ths.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.