当前位置:网站首页>Example 006: Fibonacci series

Example 006: Fibonacci series

2022-07-05 08:22:00 Lazy smile

example 006: Fibonacci sequence

subject : Fibonacci sequence . Output Fibonacci sequence of a given number .

# Topic analysis : Fibonacci sequence (Fibonacci sequence), from 1,1 Start , Each of the latter is equal to the sum of the first two .

# Program analysis : We can define a list to store Fibonacci series , According to the law of Fibonacci sequence , From 3 A start , Each item is the sum of the first two items , Get the sum of the first two items and store it in the sequence .

#  When you first enter  i=0 When ,a=1,b=a+b=2,
 When i=1 when ,a=2,b=a+b=3, When i=2 when ,a=3,b=a+b=5,·······
list_fib = [1]  #  Define a list to store Fibo's number series , Initialize the value of the first element as 1

target = int(input(" Enter the perbonacci sequence of the first few items you want "))
res = 0
a, b = 1, 1

for i in range(target - 1):
    a, b = b, a + b
    list_fib.append(a)

print(list_fib)

Add the above paragraph to the cycle, and we can test it many times to see the effect :

for j in range(5):

    list_fib = [1]  #  Define a list to store Fibo's number series , Initialize the value of the first element as 1

    target = int(input(" Enter the perbonacci sequence of the first few items you want "))
    res = 0
    a, b = 1, 1

    for i in range(target - 1):
        a, b = b, a + b
        list_fib.append(a)

    print(list_fib)

 

原网站

版权声明
本文为[Lazy smile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050816109747.html