No idea how to solve SICP exercise 1.11

后端 未结 6 1623
感动是毒
感动是毒 2021-02-01 03:59

Exercise 1.11:

A function f is defined by the rule that f(n) = n if n < 3 and f(n) = f(n - 1) + 2f(n - 2)

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 04:26

    I think you are asking how one might discover the algorithm naturally, outside of a 'design pattern'.

    It was helpful for me to look at the expansion of the f(n) at each n value:

    f(0) = 0  |
    f(1) = 1  | all known values
    f(2) = 2  |
    
    f(3) = f(2) + 2f(1) + 3f(0)
    f(4) = f(3) + 2f(2) + 3f(1)
    f(5) = f(4) + 2f(3) + 3f(2)
    f(6) = f(5) + 2f(4) + 3f(3)
    

    Looking closer at f(3), we see that we can calculate it immediately from the known values. What do we need to calculate f(4)?

    We need to at least calculate f(3) + [the rest]. But as we calculate f(3), we calculate f(2) and f(1) as well, which we happen to need for calculating [the rest] of f(4).

    f(3) = f(2) + 2f(1) + 3f(0)
                ↘       ↘
    f(4) = f(3) + 2f(2) + 3f(1)
    

    So, for any number n, I can start by calculating f(3), and reuse the values I use to calculate f(3) to calculate f(4)...and the pattern continues...

    f(3) = f(2) + 2f(1) + 3f(0)
                ↘       ↘
    f(4) = f(3) + 2f(2) + 3f(1)
                ↘       ↘
    f(5) = f(4) + 2f(3) + 3f(2)
    

    Since we will reuse them, lets give them a name a, b, c. subscripted with the step we are on, and walk through a calculation of f(5):

      Step 1:    f(3) = f(2) + 2f(1) + 3f(0) or f(3) = a1 + 2b1 +3c1
    

    where

    a1 = f(2) = 2,

    b1 = f(1) = 1,

    c1 = 0

    since f(n) = n for n < 3.

    Thus:

    f(3) = a1 + 2b1 + 3c1 = 4

      Step 2:  f(4) = f(3) + 2a1 + 3b1
    

    So:

    a2 = f(3) = 4 (calculated above in step 1),

    b2 = a1 = f(2) = 2,

    c2 = b1 = f(1) = 1

    Thus:

    f(4) = 4 + 2*2 + 3*1 = 11

      Step 3:  f(5) = f(4) + 2a2 + 3b2
    

    So:

    a3 = f(4) = 11 (calculated above in step 2),

    b3 = a2 = f(3) = 4,

    c3 = b2 = f(2) = 2

    Thus:

    f(5) = 11 + 2*4 + 3*2 = 25

    Throughout the above calculation we capture state in the previous calculation and pass it to the next step, particularily:

    astep = result of step - 1

    bstep = astep - 1

    cstep = bstep -1

    Once I saw this, then coming up with the iterative version was straightforward.

提交回复
热议问题