欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

迭代法计算斐波那契数列

程序员文章站 2022-05-25 09:27:07
1 1 1 2 3 5 8 first second result first second result (N>2) ......
    def f(n):
        second = 1
        result = 1
        while (n>2):
            n-=1
            first = second
            second = result
            result = second +first
        print result
        return result
[f(i) for i in range(16)]
out[15]: [1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

 

 1    1      1     2    3     5    8

first     second       result

      first            second    result  (n>2)