Last active
December 26, 2015 19:29
-
-
Save martynchamberlin/7202257 to your computer and use it in GitHub Desktop.
Here's a pretty decent implementation of the fibonacci sequence. It's taken from pseudocode from my Discrete Mathematics textbook and modified to be compliant with the true sequence (see lines 7 and 8). I'm not getting any console errors when I call the function without the second parameter so it's all good. Wish Javascript made optional paramet…
This file contains 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
function fibonacci(n, recursive) | |
{ | |
if ( n == 0 ) | |
return 0; | |
if ( n < 3 ) | |
return 1; | |
if ( recursive !== true ) | |
n--; | |
return fibonacci( n-1, true) + fibonacci( n-2, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment