Created
February 3, 2015 14:58
-
-
Save AllThingsSmitty/f5c85dd4d42a00fde8e7 to your computer and use it in GitHub Desktop.
Fibonacci sequence printed
This file contains hidden or 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
// 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