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?
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: