What is the Fibonacci sequence?
--a group of values starting from the third value, each equal to an interesting sequence of the sum of the first two values
such as [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
How do I implement it with a program?
--Logical finishing
Initial value n_1 = 1, n_2 = 1
N_3 = n_1 + n_2
The third value starts with each value being the sum of the first two values, if n_1 = n_2, n_2 = N_3,
Implement the overall n_1, n_2 move back a number, that is, now the n_1 +n_2 equivalent to N_2 + n_3
Although the results to n_3 received, but N_3 is essentially the fourth number, it may be the following number is so, Jiaxiang n_3 infinite Backward move
N_3 can replace any subsequent number.
--Programmatic:
List_fab = [] # definition Receive fab value list Def fab (n): n_1, n_2 = 1, 1 # define initial value n_1=1,n_2=1 list_fab.append (n_1) # put two first The starting value is added to the Fab value list list_fab.append (n_2) for I in Range (n-2): # As input requires a few minus 2 because there are two initial values n_3 = n_1 + n_2 # Third value = The sum of the first two values list_fab.append (n_3) # Adds the added value to the FAB value list n_1, n_2 = n_2, n_3 # n_1 and n_2 synchronously move backward one return List_fab # return result = Fab ( #) # Receive results print (List_fab, Len (list_fab)) # Print Results
This implementation is forward derivation, no sub-problem partitioning-deduced from the back
Python_ Fibonacci Sequence