Created
February 13, 2015 09:56
-
-
Save martinpinto/303771b7373a66e844c4 to your computer and use it in GitHub Desktop.
This file contains 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
/* iterative fibonacci sequence O(n) */ | |
function fibo(n) { | |
var fiboArr = [0, 1]; | |
for (var i = 1; i < n; i++) { | |
fiboArr.push(fiboArr[i - 1] + fiboArr[i]); | |
} | |
return fiboArr; | |
} | |
function fiboSum(n) { | |
var sum = 0, | |
arr = fibo(n); | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] > n) | |
break; | |
if (arr[i] % 2 == 0) { | |
sum += arr[i]; | |
} | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment