Created
November 2, 2015 20:19
-
-
Save DanielKucal/ec13d00acec2c27092be 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
/** | |
* 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