So we can see what it's doing is it runs. So let's try fib it here with Fib of 6.
我们可以看到它在,运行的时候做了什么,让我们试试。
fib So if I think about computing Fib of I'm going to be computing that a lot of times.
所以如果我想计算,我得算上很多次。
The even better news is that Fib instead of 25 calls, it was only 11 calls.
更好的消息是,上次调用了25次,这次只调用了11次。
And the memo will be a dictionary that maps me from a number to Fib of that number.
这个memo就是我从一个fib数,到另一个fibs数的记录字典。
fib So I'll create Fib 1, fib1 which has the same arguments as Fib.
所以我将会创建和,有相同参数的。
When I ran it up here, with the old Fib, and we printed the result, and I ran it with Fib 1 down here.
当我运行到这里,也就是老的Fib函数,我们打印这个结果,当我在下面调用Fib1时,也打印了结果。
fib Roughly speaking, the analysis of fib is actually quite complex, of a recursive fib.
简单的说,递归的fib的分析实际上非常复杂。
And I've done fib of 5, which is a small number. If we look at some other things, for example, let's get rid of this.
我已经计算了fib,这是一个小数,如果我们看看其它的东西,例如,让我们放开这个。
And then I'll call fast Fib and it returns the result it has.
然后我会调用快速Fib函数,然后它会返回它的结果。
So, as we would hope, fib=8 That right? That right, everybody?
正如我们期望的那样,对的吗?,Fib,of,6,happens,to,be,8。,对的吗,回答一下?
But first I'm going to have, let's look at what fast Fib does and then we'll come back to the next question.
但首先我将会,让我们看看快速fib算法,做了些什么然后,我们再来讨论下一个问题。
And let's see what happens if we call fib of 5.
让我们看看我们调用Fib会怎么样。
fib I don't think so.
是等于8吗?,Should,Fib,of,6,be,8?,我不这么认为。
Before we do that, or as you do that, let's look at Fib 1. The key thing to notice about Fib 1 is that it has the same specification as Fib.
我们做之前,或者当你在思考的过程中,让我们看看Fib,1,Fib1重要注意的,关键的一点就是,他和Fib的格式描述是一样的。
Because Fib of is? What?
因为fib的值是?什么?
I have used divide and conquer, as we seen before, to recursively break it into smaller problems. But the smaller problem of fib of 4 and the smaller problem of fib of 3 overlap with each other.
正如我们之前看到的,我已经进行了划分,并且递归性的把它分解为更小的问题,但是fib的简化问题,和fib的简化问题会相互重叠。
So I'm going to have something called fast Fib.
我将要运行的是快速fib算法。
Here fib got called 177 times.
这个fib函数被调用了177次。
Now, fortunately, Fib of 0 is short.
现在,幸运的是fib很短。
应用推荐