归档 2014年12月1日

Python非递归算法求解Fibonacci斐波那契数列

斐波那契数列递归数学定义如下:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)

Python代码:

函数f(n),输入非负整数n,返回f[n] 

def f(n):
    a, b = 0, 1
    for i in range(0, n):
        a, b = b, a + b
    return a

函数返回值:

print(f(1))  # 1
print(f(2))  # 1
print(f(3))  # 2
print(f(10)) # 55

上述代码来自:http://stackoverflow.com/questions/15047116/a-iterative-algorithm-for-fibonacci-numbers

函数fib(n),输入非负整数n,返回fibs数组

def ...

继续阅读

昨天

明天

归档