BA new pick is wanted each time, so a kept one is wrong.
CIt depends on the time now as well.
pen and paper
5question 5about 6 minutes
Run the method by hand on F(5).
List every call in order, marking those the memo answers.
What does the memo hold at the end?
How many calls are there, and how many are answered?
a hint
F(k) asks for F(k - 1) first, and F(k - 2) only when that has ended.
a worked answer
F(5), F(4), F(3), F(2), F(1), F(0), then F(1) answered, F(2) answered and F(3) answered. The memo ends 0, 1, 1, 2, 3, 5. There are 9 calls, and 3 are answered.
A friend says a memo can save at most half the calls. Their reason is that each number asks for two, and only the second is answered.
How many calls does the plain method make on F(20)?
How many does the method with a memo make?
Where does your friend go wrong?
a hint
Think about what the plain method does under a call it repeats.
a worked answer
The plain method makes 2 times F(21), less 1. F(21) is 10,946, so 21,891 calls. With a memo, 2 times 20, less 1, which is 39. Your friend counts the answered call and forgets everything under it. In the plain method that call makes all its own calls again, and they go too.
Question 4: exactly how many calls does the plain method make? Let be , the place one on from .5 of 5 shown
1the move: add 1 to each side
the line:
why it is allowed: From 2 up, the calls for are 1, for the first call, plus the calls for and for . Add 1 to each side, and the right has one 1 for each bracket. For 6, 25 + 1 is (15 + 1) + (9 + 1).
2the move: the rule again
the line:
n
calls + 1
half of that
F(n + 1)
0
2
1
1
1
2
1
1
2
4
2
2
3
6
3
3
4
10
5
5
5
16
8
8
6
26
13
13
Calls plus 1, halved, is the next Fibonacci number.
why it is allowed: That is the rule of the Fibonacci numbers themselves. Calls + 1 starts 2, 2, then 4, 6, 10. Halved, that is 1, 1, 2, 3, 5.
guess it, then
3the move: name the place
the line:
why it is allowed: At 0 and 1, halved, it gives F(1) and F(2), and each row goes on the same, the Fibonacci number one place on from . For 6, is 7.
guess it, then
4the move: twice that
the line:
why it is allowed: Twice F(1) and twice F(2) start it, and each later one adds the two before it, as the Fibonacci numbers do. So it stays twice. For 6, 26 is 2 times 13.
guess it, then
5the move: the calls
the line:
why it is allowed: Take the 1 back off. For 6, F(7) is 13, so 25. So the plain method's calls grow as fast as its answers do, as the books say (volume three, chapter 38). Counted, from 20 up, each 1 more multiplies them by about 1.618, as 13,529 calls at 19 become 21,891 at 20.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand