(function () {
var i;
var fib = []; //Initialize array!
fib[0] = 0;
fib[1] = 1;
for (i = 2; i <= 100; i++) {
// Next fibonacci number = previous + one before previous
// Translated to JavaScript:
fib[i] = fib[i - 2] + fib[i - 1];
document.getElementById("sequence").innerHTML += (2 < i ? ", " : "") + fib[i];
}
}());
(function () {
fib = function (numMax) {
var i, j, k, x;
for (i = 0, j = 1, k = 0; k < numMax; i = j, j = x, k++) {
x = i + j;
document.getElementById("sequence2").innerHTML += (0 < i ? ", " : "") + x;
}
};
fib(100);
}());
(function () {
/**
* Binet Fibonacci number formula for determining
* sequence values
* @param {int} pos - the position in sequence to lookup
* @returns {int} the Fibonacci value of sequence @pos
http://stackoverflow.com/questions/7944239/generating-fibonacci-sequence/32079629#32079629
*/
var fib = function (pos) {
return Math.floor((Math.pow( 1 + Math.sqrt(5), pos)
- Math.pow( 1 - Math.sqrt(5), pos))
/ (Math.pow(2, pos) * Math.sqrt(5)));
};
//value of highest position I can see in Linux/Chrome 41.0
document.getElementById("sequence3").innerHTML = fib(10); // Incorrectly returns 54
}());
(function () {
/**
* Binet Fibonacci number formula for determining
* sequence values
* @param {int} pos - the position in sequence to lookup
* @returns {int} the Fibonacci value of sequence @pos
modded from above code
*/
var fib = function (pos) {
var floor1, pow1, pow2, pow3;
pow1 = Math.pow(1 + Math.sqrt(5), pos) * 100 / 100;
pow2 = Math.pow(1 - Math.sqrt(5), pos) * 100 / 100;
pow3 = Math.pow(2, pos) * Math.sqrt(5) * 100 / 100;
return Math.floor((pow1 - pow2) / pow3);
};
//value of highest position I can see in Linux/Chrome 41.0
document.getElementById("sequence4").innerHTML = fib(10); //...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.