Skip to content

Instantly share code, notes, and snippets.

@martinpinto
Created February 13, 2015 09:56
Show Gist options
  • Save martinpinto/303771b7373a66e844c4 to your computer and use it in GitHub Desktop.
Save martinpinto/303771b7373a66e844c4 to your computer and use it in GitHub Desktop.
/* 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