#2: def fib(n): if n<=1: return n else: return(fib(n-1) + fib(n-2)) print(fib(10
ID: 3780170 • Letter: #
Question
#2:
def fib(n):
if n<=1:
return n
else:
return(fib(n-1) + fib(n-2))
print(fib(10))
print(fib(20))
print(fib(30))
print(fib(40))
print(fib(50))
3. (25 points) We can determine how many digits a positive integer has by repeatedly dividing by 10 (without keeping the remainder) until the number is less than 10, consisting of only 1 digit. We add 1 to this value for each time we divided by 10. Here is the recursive algorithm: 1. If n 10 return 1 2. Otherwise, return 1 the number of digits in n/10 (ignoring the fractional part). Implement this recursive algorithm in Python and test it using a main function that calls this with the values 22, 220221, and fib (20 (from #2)Explanation / Answer
def fib(n):
if n<=1:
return n
else:
return(fib(n-1) + fib(n-2))
def digits(n):
if(n<10):
return 1;
return 1+digits(n/10)
print(digits(22))
print(digits(220221))
print(digits(fib(20)))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.