Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Created February 3, 2015 14:58
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 AllThingsSmitty/f5c85dd4d42a00fde8e7 to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/f5c85dd4d42a00fde8e7 to your computer and use it in GitHub Desktop.
Fibonacci sequence printed
// Example sequence: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610...
// Get Fibonacci sequence with a loop
var looping = function (n) {
'use strict';
var a = 0, b = 1, f = 1;
for (var i = 2; i <= n; i++) {
f = a + b;
a = b;
b = f;
}
return f;
};
// Get Fibonacci sequence with recursion
var recursive = function (n) {
'use strict';
if (n <= 2) {
return 1;
} else {
return this.recursive(n - 1) + this.recursive(n - 2);
}
};
//recursive(4) + recursive(3)
//(recursive(3) + recursive(2)) + (recursive(2) + recursive(1))
//((recursive(2) + recursive(1)) + 1) + (1 + 1)
//((1 + 1) + 1) + (1 + 1)
//5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment