Hi. I want to understand step by step how the code below produces the Fibonnaci numbers.
Particularly the for loop.
def fib(n):
if n < 1:
return None
if n < 3:
return 1
elem_1 = elem_2 = 1
the_sum = 0
for i in range(3, n + 1):
the_sum = elem_1 + elem_2
elem_1, elem_2 = elem_2, the_sum
return the_sum
For example our n is 10.
elem_1 = elem_2 = 1
the_sum = 0
for i in range(3, 11):
the_sum = elem_1 + elem_2
elem_1, elem_2 = elem_2, the_sum
What values do elem_1 and elem_2 take at a particular range…