A few years ago I was writing a web app to enter into the5k project competition. The rules of the competition was to create a functioning web app less than 5k (including external files). The app I decided to create was to generate a pattern based on a mathematical function (either the Lorenz attractor, or a spirograph) through DOM manipulation.

Whilst developing this app I created the following Javascript function that returns the nth number within the Fibonacci sequence, which I’ve recently contributed to a Wikipedia article.

function fibonacci(n)
{
var Phi=1.6180339887498948482;
var fibonacciNumber=0;
fibonacciNumber=Math.pow(Phi,n)/(Math.sqrt(5));
return Math.round(fibonacciNumber);
}

Here’s another method of calculating fibonacci(n)

function fibonacci(n)
{
var Phi=1.6180339887498948482;
var x=0;
var y=0;
var fibonacciNumber=0;
x=Math.pow(Phi,n);
y=Math.pow(-Phi,-n);
fib=(x-y)/Math.sqrt(5);
return Math.round(fibonacciNumber);
}

Example call of the above functions;

fibonacci(10)

returns the 10th number within the series which is 55.

These functions are only accurate to calculating the 70th number within the Fibonacci sequence, which is 190392490709135.