打印100以内的斐波那契数列、求第101项

这里只说一下简单的循环打印,递归之后再说。

a = 0
b = 1
print(a)
print(b)
while True:
    c = a + b
    if c > 100:
        break
    a = b
    b = c
    print(c)
0
1
1
2
3
5
8
13
21
34
55
89
求第101项
a = 0
b = 1
# 手动打印前两项
print('{},{}'.format(0, a))
print('{},{}'.format(1, b))
index = 1
while True:
    c = a + b
    a = b
    b = c
    index += 1
    print('{},{}'.format(index, c))
    if index == 101:
        break
0,0
1,1
2,1
3,2
4,3
5,5
6,8
7,13
8,21
9,34
...
95,31940434634990099905
96,51680708854858323072
97,83621143489848422977
98,135301852344706746049
99,218922995834555169026
100,354224848179261915075
101,573147844013817084101
posted @ 2018-08-04 15:23  KeithTt  阅读(2810)  评论(0编辑  收藏  举报