首页 > 其他 > 详细

迭代法计算斐波那契数列

时间:2019-10-04 13:46:52      阅读:148      评论:0      收藏:0      [点我收藏+]
    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)

 

 
 
 
 
 
 
 
 
 
 

迭代法计算斐波那契数列

原文:https://www.cnblogs.com/qtnt/p/11621928.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!