Last active
September 27, 2019 01:01
-
-
Save scriptonian/ed33c72275932f258620b3c0cf9693bd to your computer and use it in GitHub Desktop.
Fibonacci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var COUNT = 40; | |
/* Run Fibonnaci using the iterative approach*/ | |
function iterativeFibonacci(n) { | |
var firstPrevious = 0, | |
secondPrevious = 1, | |
nextSeqNumber; | |
//stop the first and second numbers | |
var results = [firstPrevious, secondPrevious]; | |
//if n is less than 2 return the passed in param | |
if(n < 2) return n; | |
//start iteration | |
for(var i = 2; i < n; i++) { | |
nextSeqNumber = firstPrevious + secondPrevious; | |
//push next number into results | |
results.push(nextSeqNumber); | |
//update the previous numbers | |
firstPrevious = secondPrevious; | |
secondPrevious = nextSeqNumber; | |
} | |
return results; | |
} | |
//TEST FUNCTION | |
console.time('Iterative Fibonacci'); | |
//call the iterative function | |
var itSequence = iterativeFibonacci(COUNT); | |
console.timeEnd('Iterative Fibonacci'); | |
/* Run Fibonnaci using the resursive approach*/ | |
function recursiveFibonacci(n) { | |
if(n < 2) return n; | |
//recursively call the function | |
return recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2); | |
} | |
//TEST FUNCTION | |
console.time('Recursive Fibonacci'); | |
//return the result of adding the sequence numbers | |
var recSequence = recursiveFibonacci(COUNT); | |
//display the sequence number | |
var recursiveCount = 0; | |
while(recursiveCount < COUNT) { | |
recursiveFibonacci(recursiveCount); | |
recursiveCount++; | |
} | |
console.timeEnd('Recursive Fibonacci'); | |
/* | |
Fibonacci Using Dynamic Programming | |
*/ | |
function DPFibonacci(n) { | |
var sequence = []; | |
sequence[0] = 0; | |
sequence[1] = 1; | |
if(n < 2) return n; | |
for(var i = 2; i < n; i++) { | |
sequence[i] = sequence[i - 1] + sequence[i - 2]; | |
} | |
return sequence; | |
} | |
//TEST FUNCTION | |
console.time('Dynamic Programming Fibonacci'); | |
//call the iterative function | |
var dpSequence = DPFibonacci(COUNT); | |
console.timeEnd('Dynamic Programming Fibonacci'); | |
/* | |
Fibonacci Using Memoization | |
*/ | |
function memoizeFibonacci(n) { | |
var cache = {}; | |
function fibonacci(n) { | |
let result; | |
if(cache[n]) { | |
result = cache[n]; | |
} else { | |
if(n < 2) return n; | |
//recursively call the function | |
result = fibonacci(n - 1) + fibonacci(n - 2); | |
//save the return to result | |
cache[n] = result; | |
} | |
return result; | |
}; | |
return fibonacci(n); | |
} | |
//TEST FUNCTION | |
console.time('Memoized Fibonacci'); | |
var memFibonacci = memoizeFibonacci(COUNT); | |
console.timeEnd('Memoized Fibonacci'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment