MathsNumber Theory

Fibonacci
calculator.

Find any Fibonacci number exactly, list a run of terms, or test a number you have.

How it's worked out

Each term is the sum of the two before it. The sequence starts from F(0) = 0 and F(1) = 1, so it runs 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and on without end.

F(n)\displaystyle F(n)=F(n−1)+F(n−2)\displaystyle {}= F(n-1) + F(n-2)

Adding one term at a time takes n steps, which is slow for a large n. Fast doubling skips ahead instead. From F(k) and F(k + 1) it gets F(2k) and F(2k + 1) in one go, so each step halves the distance. F(100,000) has 20,899 digits and takes about 17 of these steps. Every digit is kept, because the tool works in whole numbers of any length rather than in decimals.

F(2k)\displaystyle F(2k)=F(k)(2F(k+1)−F(k))\displaystyle {}= F(k)\,\bigl(2F(k+1) - F(k)\bigr)
F(2k+1)\displaystyle F(2k+1)=F(k)2+F(k+1)2\displaystyle {}= F(k)^2 + F(k+1)^2

Binet's formula gives the same terms from powers of the golden ratio φ, which is about 1.618034. MathWorld writes it as a difference of two powers over √5. The second power shrinks toward 0, so rounding the first alone gives the term. For n = 30 that rounding gives 832,040, which is F(30).

F(n)\displaystyle F(n)=round⁡(φn5)\displaystyle {}= \operatorname{round}\left(\frac{\varphi^{n}}{\sqrt{5}}\right)

Testing a number uses a fact recorded in the OEIS entry. A whole number x is a Fibonacci number exactly when 5x² + 4 or 5x² − 4 is a perfect square. The tool takes exact integer square roots, so the test still holds for a number thousands of digits long.

5x2+4\displaystyle 5x^{2} + 4=k2\displaystyle {}= k^{2}or\displaystyle \text{or}5x2−4\displaystyle 5x^{2} - 4=k2\displaystyle {}= k^{2}

Worked examples

Priya checks her code for F(100)

Priya's program prints the 100th Fibonacci number, and she wants to know if it is right. F(100) = 354,224,848,179,261,915,075, a number with 21 digits. It is the sum of F(99) = 218,922,995,834,555,169,026 and F(98) = 135,301,852,344,706,746,049.

If her program uses unsigned 64-bit integers, it can only hold terms up to F(93). Past that point the value wraps around and prints nonsense. In double-precision floating point, the kind most languages use for decimals, the last exact term is F(78), so a program that prints F(100) from doubles gets the first digits right and the rest wrong.

Open this example in the tool

Is 102,334,155 a Fibonacci number?

Leo found 102,334,155 in a puzzle book and suspects it is a Fibonacci number. Five times its square, plus 4, is 52,361,396,397,820,129. That is 228,826,127 squared, a perfect square, so the number is in the sequence.

Its place is F(40). Working out F(40) directly gives 102,334,155, the same number. With a long number a decimal square root rounds, which is why the test has to be done in whole numbers.

Open this example in the tool

Writing 100 as Fibonacci numbers

A student is asked to write 100 as a sum of Fibonacci numbers, using no two terms that sit next to each other. 100 is not itself a term: it falls between 89 and 144.

Take the largest term that fits each time. That gives 100 = 89 + 8 + 3, and the parts add back to 100. Zeckendorf's theorem says every positive whole number has exactly one sum like this.

Open this example in the tool

Reading your result

Check how your source counts. This tool, the OEIS and most textbooks use F(0) = 0. If a course calls 0 the first term, then its 100th term is F(99) = 218,922,995,834,555,169,026, one step back from F(100).

The ratio swings around φ. F(6) ÷ F(5) is 1.6, which is below φ, and F(11) ÷ F(10) is 1.618182, just above it. Each pair lands on the other side of φ and a little closer. So the ratios zigzag at first and then flatten out.

Negative indexes alternate in sign. Running the rule backward gives F(−n) = (−1)^(n + 1) F(n). So F(−8) = −21, while F(8) = 21.

Big answers are rounded on screen only. F(1,000) has 209 digits. The answer is shown rounded to 5 significant figures, 4.3467 × 10^208, and the full number is listed digit by digit.

Lucas numbers come along for free. They follow the same rule but start from 2 and 1, and each one is the sum of the Fibonacci terms either side: L(10) = F(9) + F(11) = 123. The check in the working uses another identity, Cassini's: F(9) × F(11) − F(10)² = 1. The answer is always 1 or −1.

Common mistakes

  • Counting from 1 when the source counts from 0. An index that is off by one gives a neighbor of the term you want, and it looks just as plausible.
  • Trusting Binet's formula in decimals. Rounding φ to the power n over √5 in double precision first gives a wrong term at n = 71. Use it as a check, not as the method.
  • Using floating point for big terms. Ordinary numbers hold whole values exactly only up to 2^53, so every term after F(78) gets rounded. Spreadsheets have the same limit.
  • Forgetting that 1 appears twice. F(1) and F(2) are both 1, so "which term is 1?" has two answers. The test reports F(1).
  • Calling 144 a coincidence. F(12) = 144 is 12 squared, and it is the only square in the sequence above 1, as J. H. E. Cohn proved in 1964.

Questions people ask

Both starts are in use, and they give the same numbers. Starting from 0 makes F(0) = 0 and F(1) = 1, which keeps identities such as F(2n) = F(n) × L(n) tidy. Starting from 1, 1 just drops the 0, so check which one your course or library expects before you compare indexes.

Sources