Я пытаюсь интерпретировать этот код от geeksforgeeks:
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
def countWays(s):
return fib(s + 1)
s = 4
print "Number of ways = ",
print countWays(s)
Кажется, я не могу получить правильный ответ, когда подставляю числа в функцию.Вот мой мыслительный процесс:
def countWays(s):
return fib(4 + 1) #5
#this goes into fib(n). 5 is not less than or equal to n so I skip to the else statement:
fib(5-1) + fib(5-2)
#fib(4) + fib(3) since these are still not less than or equal to n I input
#them into the function again.
fib(4-1) + fib(3-2)
#fib(3) + fib(1) now that one of the n is less than or equal to n,
#I return n and get 4 (3+1). The answer is supposed to be 5.