Skip to content

Instantly share code, notes, and snippets.

@DanielKucal
Created November 2, 2015 20:19
Show Gist options
  • Save DanielKucal/ec13d00acec2c27092be to your computer and use it in GitHub Desktop.
Save DanielKucal/ec13d00acec2c27092be to your computer and use it in GitHub Desktop.
/**
* Sum all odd Fibonacci Sequence elements less than given number
* Return the sum of all odd Fibonacci numbers up to and including the passed number if it is a Fibonacci number.
*/
function sumOddFibs(num) {
var sum = 0;
var fibs = [1, 1];
while (Math.max.apply(Math, fibs) < num) {
fibs.push(fibs[fibs.length-1]+fibs[fibs.length-2]);
}
for (var i=0; fibs[i] <= num && i < fibs.length; i++) {
if (fibs[i] % 2) {
sum += fibs[i];
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment