Skip to content

Instantly share code, notes, and snippets.

@theWhiteFox
Created July 2, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theWhiteFox/e6a9003c4ddd678d6279190c26056151 to your computer and use it in GitHub Desktop.
Save theWhiteFox/e6a9003c4ddd678d6279190c26056151 to your computer and use it in GitHub Desktop.
// for loop
function fibFor() {
var a = 0, b = 1, i = 1, result;
result = b;
console.log(a + '\n' + result + '\n');
for(i; i < 10; i++) {
console.log(result + '\n');
result = a + b;
a = b;
b = result;
}
}
// recursive starts at 0
function fib(number) {
if(number == 0) return 0;
if(number == 1) return 1;
return fib(number - 2) + fib(number - 1);
}
// shorter recursive starts at 1
function fibRecursive(n) {
if(n <= 1) return 1;
return fibRecursive(n - 2) + fibRecursive(n - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment