Fibonacci Bunnies Numbers with Python

python day  23

There are so many interesting stories and wonderful applications about the Fibonacci numbers. The origin of Fibonacci number is considering the growth of an idealized rabbit population growth. Assuming the rabbits can breed forever, each breeding pair mates at the age of one month, and at the end of their second month, they always produce another pair of rabbits.

Fibonacci Puzzle: How many pairs of bunnies will there be in one year? 

$${\displaystyle F_{0}=0}$$

$${\displaystyle F_{1}=1}$$

$${\displaystyle F_{n}=F_{n-1}+F_{n-2}}({n}\geq{2})$$

Fibonacci numbers are easier to calculate by hand, but now we will use Python to create a Fibonacci Numbers.

  • Fibonacci Number: Basic method

def fibonacci(n):
    f0=0
    f1=1
    for i in range (n):
        f0,f1= f1, f0+f1
        print(f0, end=" ")

fibonacci(20)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

 

  •  Fibonacci Number with Yield

def fibonacci1(n):
    a,b=0,1
    for i in range (n):
        a,b =b, a+b
        yield a
        
def fibo_number(n):
    for x in fibonacci1(n):
        print(x)

fibonacci1(10)
fibo_number(10)
1
1
2
3
5
8
13
21
34
55

Happy study!

一定要等到忘羡一曲重逢的时候,
一定要去云深不知处,
数数有多少姑苏小白兔。

 

reference:

https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md

https://en.wikipedia.org/wiki/Fibonacci_number

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: