Last active
September 21, 2021 23:21
-
-
Save omeiza/a8c62849cd983f82c2b24d2e427b15ee to your computer and use it in GitHub Desktop.
Project Euler Sum of even valued fibonacci sequence
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
// Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: | |
// 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... | |
// By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. | |
function fiboAddEven(start, limit) { | |
if ((typeof start !== "number" || start === 0) || typeof limit !== "number") return "Arguments supplied to fn fiboEvenAdd must be a natural integer"; | |
let fibArray = []; | |
fibArray.push(start); | |
for (let i = 1; i < limit; i++) { | |
let former = fibArray[fibArray.length - 2], | |
later = fibArray[fibArray.length - 1]; | |
// add first item (1) to itself | |
if (fibArray.length == 1) former = later; | |
// check if last two items add up to exceed limit | |
if ((former + later) > limit) break; | |
fibArray.push(former + later); | |
} | |
// filter out all items that are even and add them up | |
let result = 0; | |
fibArray.forEach(function(eachItem){ | |
if (eachItem % 2 === 0) result += eachItem; | |
}); | |
return result; | |
} | |
// Test | |
// fiboAddEven(1, 4000000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment