If you played with the fibonacci function from Section 6.7, you might have noticed that
the bigger the argument you provide, the longer the function takes to run. Furthermore,
the run time increases very quickly.
To understand why, consider Figure 11.2, which shows the call graph for fibonacci with
n=4:
A call graph shows a set of function frames, with lines connecting each frame to the frames
of the functions it calls. At the top of the graph, fibonacci with n=4 calls fibonacci with
n=3 and n=2. In turn, fibonacci with n=3 calls fibonacci with n=2 and n=1. And so on.
Count how many times fibonacci(0) and fibonacci(1) are called. This is an inefficient
solution to the problem, and it gets worse as the argument gets bigger.
One solution is to keep track of values that have already been computed by storing them
in a dictionary. A previously computed value that is stored for later use is called a memo.
Here is an implementation of fibonacci using memos:

known = {0:0, 1:1}
def fibonacci(n):
  if n in known:
    return known[n]
  res = fibonacci(n-1) + fibonacci(n-2)
  known[n] = res
  return res

known is a dictionary that keeps track of the Fibonacci numbers we already know. It starts
with two items: 0 maps to 0 and 1 maps to 1.

<Think Python>中斐波那契使用memo实现的章节的更多相关文章

  1. Python中斐波那契数列的四种写法

    在这些时候,我可以附和着笑,项目经理是决不责备的.而且项目经理见了孔乙己,也每每这样问他,引人发笑.孔乙己自己知道不能和他们谈天,便只好向新人说话.有一回对我说道,“你学过数据结构吗?”我略略点一点头 ...

  2. Python中斐波那契数列的赋值逻辑

    斐波那契数列 斐波那契数列又称费氏数列,是数学家Leonardoda Fibonacci发现的.指的是0.1.1.2.3.5.8.13.21.34.······这样的数列.即从0和1开始,第n项等于第 ...

  3. python实现斐波那契数列(Fibonacci sequence)

    使用Python实现斐波那契数列(Fibonacci sequence) 斐波那契数列形如 1,1,2,3,5,8,13,等等.也就是说,下一个值是序列中前两个值之和.写一个函数,给定N,返回第N个斐 ...

  4. python基础----斐波那契数列

    python实现斐波那契数列的三种方法 """ 斐波那契数列 0,1,1,2,3,5,8,13,21,... """ # 方法一:while ...

  5. Python——通过斐波那契数列来理解生成器

    一.生成器(generator) 先来看看一个简单的菲波那切数列,出第一个和第二个外,任意一个数都是由前两个数相加得到的.如:0,1,1,2,3,5,8,13...... 输入斐波那契数列前N个数: ...

  6. python之斐波那契数列递归推导在性能方面的反思

    在各种语言中,谈到递归首当其冲的是斐波那契数列,太典型了,简直就是标杆 一开始本人在学习递归也是如此,因为太符合逻辑了 后台在工作和学习中,不断反思递归真的就好嘛? 首先递归需要从后往前推导,所有数据 ...

  7. python实现斐波那契数列

    https://www.cnblogs.com/wolfshining/p/7662453.html 斐波那契数列即著名的兔子数列:1.1.2.3.5.8.13.21.34.…… 数列特点:该数列从第 ...

  8. python实现斐波那契数列笔记

    斐波那契数列即著名的兔子数列:1.1.2.3.5.8.13.21.34.…… 数列特点:该数列从第三项开始,每个数的值为其前两个数之和,用python实现起来很简单: a=0 b=1 while b ...

  9. [Python3.X]python 实现斐波那契数列

    斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一 ...

随机推荐

  1. 图解Eclipse中配置Maven并创建Maven的Web工程

      打开eclipse,Windows->Preferences,如下图所示: 2 在Preferences作如下配置,如下图所示: 3 配置完Maven的安装目录后,还需要配置Maven的配置 ...

  2. Spring AOP 中pointcut expression表达式

    原文地址——http://blog.csdn.net/qq525099302/article/details/53996344 Pointcut是指那些方法需要被执行”AOP”,是由”Pointcut ...

  3. STL容器-- forward_list 用法

    http://www.cplusplus.com/reference/forward_list/

  4. simhash与Google的网页去重

    前几天去吃葫芦头的路上,大飞哥给详细的讲解了他在比较文本相似度实验时对Google的simhash方法高效的惊叹,回来特意去找了原文去拜读. Simhash 传统IR领域内文本相似度比较所采用的经典方 ...

  5. int LL数据范围

    unsigned int 0-4294967295 0~4e9 int -2147483648-2147483647 -2e9~2e9 unsigned long 0-4294967295 long ...

  6. [ 9.13 ]CF每日一题系列—— 340A GCD & LCM

    Description: [ 着实比较羞愧,都想着去暴力,把算法(方法)也忘了] A只涂x,2x,3x……,B只涂y,2y,3y……问你A和B共同涂的墙的个数 Solution: 就是求x和y的lcm ...

  7. express4.x socket

    在这个版本下使用socket,配置比较麻烦. 使用实例:http://www.open-open.com/lib/view/open1402479198587.html 配置文件:BarOrderPr ...

  8. nodeclub route

    这里是把web_router.js放在根目录下,也可以放在routes文件件下,其实都可以. 这里就是一些url与controller和middleware对应

  9. SystemProperties.get/set property_get/set

    在java层设置系统属性要注意几点: 1 需要有系统权限.可以在AndroidManifest.xml添加android:sharedUserId="android.uid.system&q ...

  10. Swift3 KWWebView的页面屏幕自适应

    lazy var webView:WKWebView = { /* NSString *jScript = @"var meta = document.createElement('meta ...