Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
50 lines (39 loc) · 1 KB

File metadata and controls

50 lines (39 loc) · 1 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# -*- coding:utf-8 -*-
__author__ = 'gjw'
__time__ = '2018/1/4 0004 下午 3:16'
# 题目:斐波那契数列。
# 程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……。
"""
在数学上,费波那契数列是以递归的方法来定义:
F0 = 0 (n=0)
F1 = 1 (n=1)
Fn = F[n-1]+ F[n-2](n=>2)
"""
# 方法一
def fib1(n):
a, b = 1, 1
for i in range(n-1):
a, b = b, a+b
return a
print(fib1(10))
# 方法二
# 递归
print("我是递归")
def fib2(n):
if n == 1 or n == 2:
return 1
return fib2(n-1)+fib2(n-2)
print(fib2(10))
# 方法三
# 如果你需要输出指定个数的斐波那契数列,可以使用以下代码:
def fib3(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(fib3(10))
Morty Proxy This is a proxified and sanitized view of the page, visit original site.