The last time Hackerfall tried to access this page, it returned a not found error. A cached version of the page is below, or clickhereto continue anyway

This code, somewhat surprisingly, generates Fibonacci numbers.

```
def fib(n):
return (4 << n*(3+n)) // ((4 << 2*n) - (2 << n) - 1) & ((2 << n) - 1)
```

In this blog post, Ill explain where it comes from and how it works.

Before getting to explaining, Ill give a whirlwind background overview of Fibonacci numbers and how to compute them. If youre already a maths whiz, you can skip most of the introduction, quickly skim the section Generating functions and then read An integer formula.

The Fibonacci numbers are a well-known sequence of numbers:

The th number in the sequence is defined to be the sum of the previous two, or formally by this recurrence relation:

Ive chosen to start the sequence at index 0 rather than the more usual 1.

Theres a few different reasonably well-known ways of computing the sequence. The obvious recursive implementation is slow:

```
def fib_recursive(n):
if n < 2: return 1
return fib_recursive(n - 1) + fib_recursive(n - 2)
```

An iterative implementation works in operations:

```
def fib_iter(n):
a, b = 1, 1
for _ in xrange(n):
a, b = a + b, a
return b
```

And a slightly less well-known matrix power implementation works in operations.

```
def fib_matpow(n):
m = numpy.matrix('1 1 ; 1 0') ** n
return m.item(0)
```

The last method works by considering the `a`

and `b`

in `fib_iter`

as sequences, and noting that

From which follows

and so if then (noting that unlike Python, matrix indexes are usually 1-based).

Its based on the assumption that numpys matrix power does something like exponentation by squaring.

Another method is to find a closed form for the solution of the recurrence relation. This leads to the real-valued formula: where and . The practical flaw in this method is that it requires arbitrary precision real-valued arithmetic, but it works for small .

```
def fib_phi(n):
phi = (1 + math.sqrt(5)) / 2.0
psi = (1 - math.sqrt(5)) / 2.0
return int((phi ** (n+1) - psi ** (n+1)) / math.sqrt(5))
```

A generating function for an arbitrary sequence is the infinite sum . In the specific case of the Fibonacci numbers, that means . In words, its an infinite power series, with the coefficient of being the th Fibonacci number.

Now,

Multiplying by and summing over all , we get:

If we let to be the generating function of , which is defined to be then this equation can be simplified:

and simplifying,

We can solve this equation for to get

Its surprising that weve managed to find a small and simple formula which captures all of the Fibonacci numbers, but its not yet obvious how we can use it. Well get to that in the next section.

A technical aside is that were going to want to evaluate at some values of , and wed like the power series to converge. We know the Fibonacci numbers grow like and that geometric series converge if , so we know that if then the power series converges.

Now were ready to start understanding the Python code.

To get the intuition behind the formula, well evaluate the generating function at .

Interestingly, we can see some Fibonacci numbers in this decimal expansion: . That seems magical and surprising, but its because .

In this example, the Fibonacci numbers are spaced out at multiples of , which means once they start getting bigger that 1000 theyll start interfering with their neighbours. We can see that starting at 988 in the computation of above: the correct Fibonacci number is 987, but theres a 1 overflowed from the next number in the sequence causing an off-by-one error. This breaks the pattern from then on.

But, for any value of , we can arrange for the negative power of 10 to be large enough that overflows dont disturb the th Fibonacci. For now, well just assume that theres some which makes sufficient, and well come back to picking it later.

Also, since wed like to use integer maths (because its easier to code), lets multiply by , which also puts the th Fibonacci number just to the left of the decimal point, and simplify the equation.

If we take this result modulo , well get the th Fibonacci number (again, assuming weve picked large enough).

Before proceeding, lets switch to base 2 rather than base 10, which changes nothing but will make it easier to program.

Now all thats left is to pick a value of large enough so that . We know that the Fibonacci numbers grow like , and , so is safe.

So! Putting that together:

If we use left-shift notation thats available in python, where then we can write this as:

Observing that can be expressed as the bitwise and (`&`

) of , we reconstruct our original Python program:

```
def fib(n):
return (4 << n*(3+n)) // ((4 << 2*n) - (2 << n) - 1) & ((2 << n) - 1)
```

Although its curious to find a non-iterative, closed-form solution, this isnt a practical method at all. Were doing integer arithmetic with integers of size bits, and in fact, before performing the final bit-wise and, weve got an integer that is the first Fibonacci numbers concatenated together!