ITPub博客

首页 > 应用开发 > > python学习-斐波那契(黄金比例)

python学习-斐波那契(黄金比例)

作者:wghdd 时间:2019-09-10 16:44:54 0 删除 编辑

修饰器,增加缓存的意义

4.如何用Python输出一个Fibonacci数列?

1a,b = 0, 12whileb<100: 3print (b), 4a, b = b, a+b

[root@localhost /]# 

[root@localhost /]# cat zlg.py 

#!/bin/python

def a(n,cache=None):

    if cache is None:

        cache={}

    if n in cache:

        return cache[n]

        print n

    if n<=1:

        return 1

    cache[n]=a(n-1,cache)+a(n-2,cache)

#    print n

#    print '**********'

#    print cache[n]

    return cache[n]

print a(5)

[root@localhost /]# 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1775/viewspace-3165615/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    109
  • 访问量
    865