0

I'm doing the Euler project problem 2 in which the objective is to sum the even numbers of the fibonacci sequence that have a value of less than 4 million. I've searched a bit and I've seen several solutions using a while loop but nothing simple using a for loop. I'm curious why I'm returning zero with the following code:

var array = [];
array[0] = 0;
array[1] = 1;

var total = 0;

for(var i=2;total<=4000000;i++) {
array[i] = array[i-1] + array[i-2];};


for(var x=0;x<array.length;x++){
if(array[x]%2 === 0){
total += array[x]};};

alert(total);

I'm guessing the problem is in my for loop using the total variable. I couldn't get it to work using array[i]<=4000000 either and I'm really curious behind the why here. Anyone know why this is? What can I change in the for loop condition (second statement) to get a correct total here?

2
  • This doesn't even terminate. The first for() will hang because the condition total<=4000000 will always be true. Commented Aug 29, 2014 at 7:39
  • Yes, the loop doesn't terminate. Nest the second for loop inside the first one. Commented Aug 30, 2014 at 9:36

3 Answers 3

1

First of all there is an infinite loop at first for. Your condition must be array[i-1] < 4000000. After that your second for loop will find the correct result.

Also for the problem, you don't need to store all fibonacci numbers then find sum of even numbers. You can calculate sum when calculating fibonacci.

var first = 0;
var second = 1;
var sum = 0;

for(var current=first+second; current < 4000000; current = first+second){
    if(current%2 === 0){
        sum+=current;
    }
    first = second;
    second = current;
}
Sign up to request clarification or add additional context in comments.

2 Comments

is it an infinite loop because array[2]<4000000 (the first loop that would be ran) uses an entry in the array [2] that hasnt yet been created or is there another reason? Also thank you for showing me the calculate as you go method. I tried that earlier but couldnt figure out how to phrase the 3rd condition of the for loop.
In your question there is total<4000000 and in the body of loop there is no operation to change total, hence total will always be 0 and loop run forever.
1

I fixed it for you.

var i, data = [ 0, 1 ], total = 0;

for (i = 2; i <= 4000000; i++)
{
    data[i] = data[i - 1] + data[i - 2];

    if (data[i] % 2 === 0)
    {
        total += data[i];
    }
}

alert(total);

I'm not sure what you termination condition should be like, you say have a value of less than 4 million, but this is ambiguous. Maybe it should be total <= 4000000 or data[i] <= 4000000. Your phrasing is not precise enough.

Comments

0

Sorry but for me your code going in a dead loop. first "for" use total as check but it's never incremented. If you want This is a solution for fibonacci sequence based on dinamic programming with memoization tecnic.

var f1 = 1;
 var f2 = 1;

 for(var i = 2; i < 40000; i++){
  console.info(i, f1, f2);
  var temp = f1 + f2;
  f1 = f2;
  f2 = temp;
 }

 alert(f2);

1 Comment

Your condition is invalid, as problem asks for fibonacci sequence which has a value of less then 4000000, your are going for 4000000th sequence. Also f0 is 0.

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.