Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active December 26, 2015 19:29
Show Gist options
  • Save martynchamberlin/7202257 to your computer and use it in GitHub Desktop.
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…
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