Skip to content

Fibonacci with a memory

planning ahead

Level 1 of 3

Also called Memoisation and Top-down dynamic programming.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

How many calls does the method make on F(6)?

the answer, and why

the answer is D

2 times 6, less 1, which is 11. F(6), F(5), F(4), F(3) and F(2) each ask for 2, which is 10, and the first call makes 11.

if you chose another

  • That is the plain method, with no memo.
  • That is how many numbers are worked out. 4 more calls are answered.
  • That is the most waiting at once.

question 2

How many calls does the memo answer on F(9)?

the answer, and why

the answer is B

One for each of F(3) to F(9), the second ask of each, which is 7.

if you chose another

  • That is the most waiting at once.
  • That is all the calls, those worked out as well.
  • That is how many numbers are worked out.

question 3

The method runs on F(5). What does the memo hold just after its second addition?

the answer, and why

the answer is C

F(2) is added first, as 1 + 0, and then F(3), as 1 + 1. Cells 0 to 3 hold 0, 1, 1 and 2.

if you chose another

  • That is after the first addition.
  • That is after the third, which adds F(4).
  • Read by cell, cell 0 holds 0. F(1) is written first, but it goes in cell 1.

question 4

Which of these can you keep in a memo, looked up by kk alone?

the answer, and why

the answer is D

The stairs depend on kk alone, so one answer serves every asker.

this tests how it works, on the learn page

if you chose another

  • The price changes, so a kept answer goes stale.
  • A new pick is wanted each time, so a kept one is wrong.
  • It depends on the time now as well.

pen and paper

question 5about 6 minutes

Run the method by hand on F(5).

  1. List every call in order, marking those the memo answers.
  2. What does the memo hold at the end?
  3. 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.

this tests how it works, on the learn page

what a full answer includes
  • The 9 calls in order, with the last 3 marked.
  • 0, 1, 1, 2, 3, 5.
  • 9 calls, 3 answered.

question 6about 6 minutes

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.

  1. How many calls does the plain method make on F(20)?
  2. How many does the method with a memo make?
  3. 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 EE be n+1n + 1, the place one on from nn.
  1. the move: add 1 to each side

    the line:
    calls for n,+1, is(calls for n−1,+1)+(calls for n−2,+1)\begin{gathered} \text{calls for } n, + 1, \text{ is} \\ (\text{calls for } n - 1, + 1) \\ + (\text{calls for } n - 2, + 1) \end{gathered}

    why it is allowed: From 2 up, the calls for nn are 1, for the first call, plus the calls for n−1n - 1 and for n−2n - 2. Add 1 to each side, and the right has one 1 for each bracket. For 6, 25 + 1 is (15 + 1) + (9 + 1).

  2. the move: the rule again

    the line:
    read down, calls + 1 addsthe two before it\begin{gathered} \text{read down, calls + 1 adds} \\ \text{the two before it} \end{gathered}
    ncalls + 1half of thatF(n + 1)
    0211
    1211
    2422
    3633
    41055
    51688
    6261313
    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.

  3. the move: name the place

    the line:
    E=n+1E = n + 1

    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 nn. For 6, EE is 7.

  4. the move: twice that

    the line:
    calls for n,+1,is 2×F(E)\begin{gathered} \text{calls for } n, + 1, \\ \text{is } 2 \times \text{F}(E) \end{gathered}

    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.

  5. the move: the calls

    the line:
    calls for n is2×F(E)−1\begin{gathered} \text{calls for } n \text{ is} \\ 2 \times \text{F}(E) - 1 \end{gathered}

    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.

try it with a small nn: at n=4n = 4, line 3 gives E=5E = 5, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 21,891 calls.
  • 39 calls.
  • An answered call also saves every call under it.