Skip to content

Instantly share code, notes, and snippets.

@mec
Created August 25, 2015 09:41
Show Gist options
  • Save mec/28d37e93a4d0d4689e0d to your computer and use it in GitHub Desktop.
Save mec/28d37e93a4d0d4689e0d to your computer and use it in GitHub Desktop.
// Fibonacci
//Looping
// seeds are 0,1,0 or 1,0,1
console.log("Looping : \n");
var result = 0,
term1 = 1,
term2 = 0;
i = 0;
console.log(result);
while(i < 20)
{
result = term1 + result;
console.log(result);
term1 = term2;
term2 = result;
i++;
}
// recursion
function fib(x) {
if (x === 0) {
return 0;
} else if (x === 1) {
return 1;
} else {
return fib(x-1)+fib(x-2);
}
}
console.log("Recursion : \n");
for (var i = 0; i <= 20; i++) {
console.log(fib(i));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment