Skip to content

Instantly share code, notes, and snippets.

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