-1

I'm trying to solve Project Euler's second problem which goes:

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.

And this is how I tried to solve it in Javascript + Why I think it's logical:

var max = 4000000; //We create a variable with the maximum amount allowed, which is 4 million.
var result = 0; //Here we create a variable to store our result.

for (var i = 0; i < max; i++) //Now let's loop through the 4 million.
{
    if (i % 2 === 0) //We're checking for even numbers here because as I understood it, we have to only use the even numbers in this problem.
    {
        result = result + i; //Here we're adding "result" (Which is 0) to itself plus *i* (Which we're looping through!). We should have the result at the end of this loop.
    }
}

console.log(result); //Print our result.

I know that Fibonacci numbers add the previous number in the row to themselves, so 1 + 2 would be 3.

According to my logic, that's what I'm doing with result = result + i, yet I'm getting the wrong answer.

I can't see the logic here.

4
  • You're adding all the even numbers. You've left out the bit where you're advancing through the Fibonacci sequence. Commented Nov 13, 2014 at 22:48
  • You're looking for even values of i. Will that give you even numbers in the Fibonacci sequence? (Hint: No) Commented Nov 13, 2014 at 22:48
  • Like brycem and Oliver said: you are adding together all the even number between 1 and 4 million. You need to first, find the Fibonacci numbers, and then second, add together all the even ones. Commented Nov 15, 2014 at 2:02
  • Oh, so that's what I've been doing wrong. Thanks! Commented Nov 15, 2014 at 15:49

1 Answer 1

1

Not every even number below 4,000,000 is in the fibonacci sequence. Try the following:

var max = 4000000, first = 1, second = 1, next = 0, result = 0;
while (second < max) {      // until we pass our limit
  next = first + second;    // find the next number in the fibonacci sequence by adding the previous two
  first = second;           // move on to the next numbers in the sequence
  second = next;
  if (second % 2 === 0)     // if the second number is even
    result += second;       // add it to our result
}
return result;              // result holds the sum of all the even fibonacci numbers below our limit!
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain the logic in this? I don't understand why this works and how it's adding the fibonacci numbers.
That is absolutely beautiful. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.