python--斐波那契数列
# 斐波那契数列 100以内
# f(n) = f(n-1) + f(n -2)
# 第一个数加第二个数等于第三个数
a = 0
b = 1
while True:
c = a + b
if c > 100:
break
a = b
b = c
print(c)
# 求斐波那契数列第101项
num = int(input(">>> 输入打印第几项:"))
n1 = 0 # 上次结果
n2 = 1 # 当前结果
tmp = 0 # 临时存放
for i in range(1, num):
tmp = n1 + n2
n1 = n2
n2 = tmp
print(n2)
###########################################
# 从1开始循环30次的斐波那契数列
# 循环次数
for_num = 30
num_01 = 0
num_02 = 1
for _ in range(for_num - 1):
num_01, num_02 = num_02, num_01 + num_02
# if num_02 < 100:
# print(num_02)
print(num_02)
#############################
# 给出一个数字,得出该范围内的所有斐波那契数列。并统计个数
scope_num = 1000 # 数字范围
count = 0 # 统计次数
num_01 = 0 # 第一个数
num_02 = 1 # 第二个数
while True:
num_01, num_02 = num_02, num_01 + num_02
if num_02 < scope_num:
print(num_02)
else:
break
count += 1
print("{} 内的个数为:{}".format(scope_num, count))
我的目标是每天厉害一点点