JSFiddle - React, Tailwind, and code Playground

by createanaccount

HTML

<div id="sequence">
    
</div>
<hr/>

<div id="sequence2">
    
</div>
<hr/>

<div id="sequence3">
    
</div>
<hr/>
<div id="sequence4">
    
</div>
<hr/>
<div id="sequence5">
    
</div>

JavaScript

(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); //...