0.2 Enter Fibonacci
Section titled “0.2 Enter Fibonacci”Al Khwarizmi’s work could not have gained a foothold in the West were it not for the efforts of one man: the 15th century Italian mathematician Leonardo Fibonacci, who saw the potential of the positional system and worked hard to develop it further and propagandize it.
But today Fibonacci is most widely known for his famous sequence of numbers
0, 1, 1, 2, 3, 5, 8, 1 3, 2 1, 3 4, … ,each the sum of its two immediate predecessors. More formally, the Fibonacci numbers F _ n are generated by the simple rule
F _ n = { c l F _ n - 1 + F _ n - 2 & text if n > 1 1 & text if n = 1 0 & text if n = 0. .No other sequence of numbers has been studied as extensively, or applied to more fields: biology, demography, art, architecture, music, to name just a few. And, together with the powers of 2, it is computer science’s favorite sequence.
In fact, the Fibonacci numbers grow almost as fast as the powers of 2: for example, F _ 3 0 is over a million, and F _ 1 0 0 is already 21 digits long! In general, F _ n ≈ 2 ^ 0 . 6 9 4 n (see Exercise0.3).
But what is the precise value of F _ 1 0 0 ; , or of F _ 2 0 0 ? Fibonacci himself would surely have wanted to know such things. To answer, we need an algorithm for computing the nth Fibonacci number.
An exponential algorithm
Section titled “An exponential algorithm”One idea is to slavishly implement the recursive definition of F _ n . Here is the resulting algorithm, in the “pseudocode” notation used throughout this book:
function fib1(n)if n = 0: return 0if n = 1: return 1return fib1(n - 1) + fib1(n - 2)Whenever we have an algorithm, there are three questions we always ask about it:
-
Is it correct?
-
How much time does it take, as a function of n?
-
And can we do better?
The first question is moot here, as this algorithm is precisely Fibonacci’s definition of F _ n . But the second demands an answer. Let T ( n ) be the number of computer steps needed to compute fib1 ( n ) ; what can we say about this function? For starters, if n is less than ^ 2 , the procedure halts almost immediately, after just a couple of steps. Therefore,
T (n) ≤ 2 text for n ≤ 1.For larger values of n , there are two recursive invocations of fib1, taking time T ( n - 1 ) and T ( n - 2 ) , respectively, plus three computer steps (checks on the value of n and a final addition). Therefore,
T (n) = T (n - 1) + T (n - 2) + 3 text for n > 1.Compare this to the recurrence relation for F _ n mathrm : we immediately see that T ( n ) ≥ F _ n
This is very bad news: the running time of the algorithm grows as fast as the Fibonacci numbers! T ( n ) is exponential in n _ : which implies that the algorithm is impractically slow except for very small values of n . .
Let’s be a little more concrete about just how bad exponential time is. To compute F _ 2 0 0 the fib1 algorithm executes T ( 2 0 0 ) ≥ F _ 2 0 0 ≥ 2 ^ 1 3 8 elementary computer steps. How long this actually takes depends, of course, on the computer used. At this time, the fastest computer in the world is the NEC Earth Simulator, which clocks 40 trillion steps per second. Even on this machine, fib1(200) would take at least 2 ^ 9 2 seconds. This means that, if we start the computation today, it would still be going long after the sun turns into a red giant star.
But technology is rapidly improving—computer speeds have been doubling roughly every 18 months, a phenomenon sometimes called Moore’s law. With this extraordinary growth, perhaps fib1 will run a lot faster on next year’s machines. Let’s see—the running time of fib1(n) is proportional to 2 ^ 0 . 6 9 4 n ≈ ( 1 . 6 ) ^ n , so it takes 1.6 times longer to compute F _ n + 1 than F _ n . And under Moore’s law, computers get roughly 1.6 times faster each year. So if we can reasonably compute F _ 1 0 0 with this year’s technology, then next year we will manage F _ 1 0 1 . And the year after, F _ 1 0 2 . And so on: just one more Fibonacci number every year! Such is the curse of exponential time.
In short, our naive recursive algorithm is correct but hopelessly inefficient. Can we do better?
A polynomial algorithm
Section titled “A polynomial algorithm”Let’s try to understand why fib1 is so slow. Figure 0.1 shows the cascade of recursive invocations triggered by a single call to fib1(n). Notice that many computations are repeated!
A more sensible scheme would store the intermediate results—the values F _ 0 , F _ 1 , … , F _ n - 1 - as soon as they become known.
function fib2(n)if n=0 return 0create an array f[0...n]f[0] = 0, f[1] = 1for i=2...n: f[i] = f[i-1] + f[i-2]return f[n]Figure 0.1 The proliferation of recursive calls in fib1.

As with fib1, the correctness of this algorithm is self-evident because it directly uses the definition of F _ n . How long does it take? The inner loop consists of a single computer step and is executed n - 1 times. Therefore the number of computer steps used by fib2 is linear in n. From exponential we are down to polynomial, a huge breakthrough in running time. It is now perfectly reasonable to compute F _ 2 0 0 or even F _ 2 0 0 , 0 0 0 .1
As we will see repeatedly throughout this book, the right algorithm makes all the difference.
More careful analysis
Section titled “More careful analysis”In our discussion so far, we have been counting the number of basic computer steps executed by each algorithm and thinking of these basic steps as taking a constant amount of time. This is a very useful simplification. After all, a processor’s instruction set has a variety of basic primitives—branching, storing to memory, comparing numbers, simple arithmetic, and so on—and rather than distinguishing between these elementary operations, it is far more convenient to lump them together into one category.
But looking back at our treatment of Fibonacci algorithms, we have been too liberal with what we consider a basic step. It is reasonable to treat addition as a single computer step if small numbers are being added, 32-bit numbers say. But the nth Fibonacci number is about 0.694n bits long, and this can far exceed 32 as n grows. Arithmetic operations on arbitrarily large numbers cannot possibly be performed in a single, constant-time step. We need to audit our earlier running time estimates and make them more honest.
We will see in Chapter 1 that the addition of two n-bit numbers takes time roughly proportional to n ; this is not too hard to understand if you think back to the grade-school procedure for addition, which works on one digit at a time. Thus fib1, which performs about F _ n additions, actually uses a number of basic steps roughly proportional to n F _ n . Likewise, the number of steps taken by fib2 is proportional to n ^ 2 , still polynomial in n and therefore exponentially superior to fib1. This correction to the running time analysis does not diminish our breakthrough.
But can we do even better than fib2? Indeed we can: see Exercise0.4.