Problems solved in full
-
The call count closed form when naive fib(10) makes 177 calls 6 steps
The tool says naive
fib(10)makes 177 calls andfib(11)makes 287. Find the closed form for the call count, prove it, and then work out how far naive Fibonacci gets in one second on a machine that manages 108 calls per second.-
Add one to each count: 178 and 288. Both are even, and their halves are 89 and 144, which are F11 and F12. So the conjecture is C(n) = 2F(n+1) β 1, with F(1) = F(2) = 1.
-
The tree explains where the doubling comes from. Every internal node makes exactly two calls and every leaf makes none, so a tree with L leaves has L β 1 internal nodes and 2L β 1 nodes altogether. The tool reports 89 base cases at n = 10, and 2 Γ 89 β 1 = 177.
-
Base cases first.
fib(0)andfib(1)return without recursing, so C(0) = C(1) = 1, and both 2F(1) β 1 and 2F(2) β 1 equal 1. -
Now the induction step. Assume the formula at n β 1 and at n β 2. The call at n is one node plus its two subtrees, and the two Fibonacci numbers underneath collapse by the definition of the sequence itself. Setting n = 11 gives 2 Γ 144 β 1 = 287, which is what the tool prints.
-
Leave the tree here. Binet's formula gives F(m) = (Οm β Οm)/β5 with Ο = 1.6180 and |Ο| < 1, so F(m) is Οm/β5 rounded to the nearest integer, and C(n) β 2Οn+1/β5. One second at 108 calls per second buys 108 calls, which fixes Οn+1 at 1.1180 Γ 108.
-
Take logarithms. ln(1.1180 Γ 108) = 18.5323 and ln Ο = 0.4812, so n + 1 = 38.51 and n = 37.5. Round down, because a fractional call does not finish.
Answer
n = 37. The tool confirms the two neighbours: 78,176,337 calls at n = 37, and 126,491,971 at n = 38. Past there, every further 1.44 in n doubles the work, because ln 2 / ln Ο = 1.44. Meanwhile the distinct-subproblem card at n = 37 reads 38. Seventy-eight million calls to answer thirty-eight questions is the argument for dynamic programming in a single line.
-
-
Three branches that grow slower than two 6 steps
Three-step stairs calls itself three times at every node; Towers of Hanoi calls itself twice. Load Stairs at 20, three branches, work out which of the two trees grows faster, and decide what the branching factor is worth as a guide.
-
Read the recurrence off the definition rather than off the picture. One call for the node itself, then three more at n β 1, n β 2 and n β 3, and anything at 2 or below returns without recursing.
-
Assume the count grows geometrically, C(n) β A xβΏ, and substitute. Divide through by x raised to n β 3 and the leading 1 is left behind as lower-order noise, which leaves a cubic. Its only real root is 1.8392868, the tribonacci constant.
-
The same recipe on the other two definitions. Fibonacci calls itself at n β 1 and n β 2, so xΒ² = x + 1 and the root is Ο = 1.6180340. Hanoi calls itself twice at n β 1, so x = 2 and there is nothing to solve.
-
Check it rather than believe it. Set n to 29, read the calls card, set n to 30, read it again, divide. Stairs gives 1.83929, Fibonacci 1.61803, Hanoi exactly 2. Five decimal places on all three.
-
What the root does not give you is the count. 1.8393Β²β° is 196,331 while the card reads 128,287, so there is a factor of 0.65 sitting in front; Hanoi's calls are exactly 2βΏβΊΒΉ β 1, a factor of 2. The base cases set that constant and the recurrence sets the root, and only the root decides who wins eventually.
-
Eventually arrives quickly here. At n = 20 Hanoi makes 16.3 times as many calls as stairs, and at n = 30 it makes 37.8 times as many. The ratio doubles every 8.3 steps, since ln 2 divided by ln(2 Γ· 1.8393) is 8.27.
Answer
Hanoi grows faster, 2 against 1.8393, and the third branch costs stairs nothing it cannot afford. The branching factor is an upper bound on the growth rate, and it is reached only when every child is one step smaller. Stairs spends two of its three calls on arguments that are 2 and 3 smaller, and those subtrees are cheap enough that they never add up to the branch it appeared to gain.
The recipe is the part to keep, because it works on a definition nobody has already solved for you. Count the children, note how far each one drops the argument, write x to the largest drop as the sum of x to the remaining drops, and take the largest real root. One caution goes with it: this is a fact about the naive tree, and the naive tree is not what anyone would write. The distinct-subproblem card reads 21 at n = 20 for stairs and 21 for Hanoi, so a memo removes the exponent altogether and the growth rate stops mattering. -
Learning path