-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticeday6.py
More file actions
69 lines (52 loc) · 1.19 KB
/
practiceday6.py
File metadata and controls
69 lines (52 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/10/24 下午11:10
# @Author : Dora
# @File : practiceday6.py
# 题目:斐波那契数列。
# 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……。
l = []
a = 0
b = 1
while a < 100000:
c = a + b
b = a
a = c
l.append(a)
print(l)
#参考答案
#方法一
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a
# 输出了第10个斐波那契数列
print(fib(10))
#方法二
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 使用递归
def fib(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2)
# 输出了第10个斐波那契数列
print (fib(10))
#方法三
#如果你需要输出指定个数的斐波那契数列,可以使用以下代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def fib(n):
if n == 1:
return [1]
if n == 2:
return [1, 1]
fibs = [1, 1]
for i in range(2, n):
fibs.append(fibs[-1] + fibs[-2])
return fibs
# 输出前 10 个斐波那契数列
print (fib(10) )