Find a Fibonacci Number using Javascript

Sat, 12/09/2017 - 13:20

Today I was watching this nifty video on Triponacci Numbers: https://youtu.be/e7SnRPubg-g and decided to better understand the math I would recreate his demonstrations using Javascript...(always helps me).

So here's my function

        // set the constant PHI
        // @param {Number}
        const PHI = (1 + Math.sqrt(5)) / 2;
        /**
         * return any Fibonacci number at position n
         * https://youtu.be/e7SnRPubg-g
         *
         * @param {Number} an integer
         * @returns {Number}
         * */
        function F(n) {
            // the formula breaks down below one...watch the video for why
            if (n == 1 || n == 1) return 1;
            /**
             * [@n-1]+[@n+1]
             * _____________
             *       5
             * */
            return (Math.round(Math.pow(PHI,n-1))+Math.round(Math.pow(PHI,n+1))) / 5; 
        }
        // F(1) = 1
        // F(2) = 1
        // F(3) = 2
        // F(4) = 3
        // F(5) = 5
        // F(6) = 8
        // F(...) = ...
        console.log(F(n));
Tags
Categories