Created
November 10, 2016 15:27
-
-
Save yvan-sraka/b5535c52e3141151836abb45e281015c to your computer and use it in GitHub Desktop.
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
// Population de lapins : [1, 1, 2, 3, 5, 8, 13 21 34 55 89 <3 <3 ! | |
// fibonnaci(0) = 1 | |
// fibonnaci(1) = 1 | |
// fibonnaci(2) = 2 | |
// fibonnaci(3) = 3 | |
// fibonnaci(4) = 5 | |
// fibonnaci(10) = 89 | |
/**************** BAD ******************/ | |
let fibonnaci = function(n) { | |
if (n < 2) { | |
return 1; | |
} | |
return fibonnaci(n - 1) + fibonnaci(n - 2); | |
}; | |
/**************** GOOD *****************/ | |
let solutions = [1, 1]; | |
let fibonnaci = function(n) { | |
if (solutions[n]) { | |
return solutions[n]; | |
} | |
solutions[n] = fibonnaci(n - 1) + fibonnaci(n - 2); | |
return solutions[n]; | |
}; | |
// TEST: | |
console.log("fibonnaci(1000) = " + fibonnaci(1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment