generated from nhcarrigan/template
30 lines
999 B
TypeScript
30 lines
999 B
TypeScript
/**
|
|
* @copyright NHCarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
|
|
/**
|
|
* 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.
|
|
* @param limit - The limit to find the Fibonacci sequence below.
|
|
* @returns The sum of the even-valued terms in the Fibonacci sequence below the limit.
|
|
* @see https://www.freecodecamp.org/learn/project-euler/project-euler-problems-1-to-100/problem-2-even-fibonacci-numbers
|
|
*/
|
|
export const fiboEvenSum = (limit: number): number => {
|
|
let current = 3,
|
|
previous = 2,
|
|
sum = 2;
|
|
|
|
while (current <= limit) {
|
|
if (current % 2 === 0) {
|
|
sum = sum + current;
|
|
}
|
|
const next = current + previous;
|
|
previous = current;
|
|
current = next;
|
|
}
|
|
return sum;
|
|
};
|