Project Euler: #2 - Even Fibonacci numbers

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will be discussing Project Euler Solutions. 'Project Euler' helps in improving problem-solving and programming skills. Remember, there is always a better solution to the problem. 💯
Problem The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? Problem Description If any given number, N is divisible by x without leaving a reminder, then x is a Factor of the Number N. Similar...
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

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.
A Fibonacci Sequence is a series in which each number is the sum of the two preceding ones.
The sequence commonly starts from 0 and 1, although some authors start the sequence from 1 and 1 or sometimes (as did Fibonacci) from 1 and 2. Starting from 0 and 1, the first few values in the sequence are
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
In our problem, it is stated that the sequence starts with 1 and 2. And the first 10 terms would be, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.
We want to find the Fibonacci sequence within 4 million and then find the sum of all the even numbers in that sequence.
This problem can be approached using a while loop and solved using the mathematical formula,

We need to find the Fibonacci Sequence that starts with 1 and 2, within 4 million. And our sequence looks like this, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ... < 4000000
Now, add the even numbers in the sequence, 2 + 8 + 34 + 144 + ... which will be our result.
let fn_1 = 1, // first
fn_2 = 2, // second
sum = 0;
while (fn_1 < 4000000) {
if (fn_1 % 2 === 0) sum += fn_1;
let fn = fn_1 + fn_2;
[fn_1, fn_2] = [fn_2, fn];
}
console.time('p#2');
console.log(sum);
console.timeEnd('p#2');

The GitHub repo for this problem is found at 02
If you have another or a better solution, please leave it in the comments below.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!