相信大家知道斐波那契数列吧,就是那个兔子繁殖的梗,而对我们写代码的,就是简单的问题了。
第1种(较简单):
import turtle as p
def febonacci(x):
a,b=0,1
for i in range(x):
a,b=b,a+b
print(a)
febonacci(12)
结果:
1
1
2
3
5
8
13
21
34
55
89
144
a先等于0,b=1,a=b,b=a+b
0+1=1 1+1=2 1+2=3 2+3=5 3+5=8 5+8=13 8+13=21 13+21=55 34+55=89 55+89=144......
第2种(较难):
import turtle
import random
from math import *
def Fibonacci_Recursion_tool(n):
if n <= 0:
return 0
elif n==1:
return 1
else: