Fibonacci Sequence (JS) - Sum of Even Numbers
Asked Answered
A

4

7

I started Project Euler. I am on problem 2 and came up with this code to come up with the sum of even fibonacci numbers up to 4 million. The code seems to do pretty much what I want it to. I do see the correct sum listed when the code is ran. The only part I am really confused about is the very last number displayed in the results. This is what it shows:

JS CODE:

var previous = 0;
var current = 1;
var sum = 0;
var next;

   for(i = 1; i < 100; i++){
        next = current + previous;
        previous = current;
        current = next; 
        if(current % 2 === 0 && current < 4000000) {
            sum += current;
        console.log(sum);
        }
   }

RESULTS:

2
10
44
188
798
3382
14328
60696
257114
1089154
4613732 (this is the number i was trying to get)
=> 354224848179262000000 (confused as to why this number shows up and what it represents)
Astrix answered 19/8, 2015 at 3:14 Comment(2)
That's not Java. Java != JavaScript.Leukorrhea
What browser are you in? The number you want is the number I get... jsbin.com/reweqalaxa/edit?js,consoleAnurag
L
6

Let me break this down:

Why does this show up?

On the console, you will see the result of any expression you execute. If you execute a block of code you will see the last expression you executed in the block. Counter intuitively, in this case it is the result of current = next because the if statement is not run on your last time through the for loop.

Why does next equal 354224848179262000000?

The hundredth Fibonacci number is 354224848179261915075. JavaScript however loses precision as your numbers get past a certain point and starts assuming that all of the lower part of your number is zeros. See this question for move details: Why does JavaScript think 354224848179262000000 and 354224848179261915075 are equal?.

Leibowitz answered 19/8, 2015 at 3:33 Comment(2)
Is there a way to fix it so the last number is the number "4613732"? Or is this fine? Technically the code was only used to find the answer, but i'd like the answer to be the final result.Astrix
It is fine. If you were running this code outside of your console you would never see that final number. It is just a choice by whoever made the console to have it show a result whenever you enter any code. You can safely ignore it. If you were writing reusable code, the entire thing would be a function and you would need to keep track of the best answer each time you got into the if block. Then after your for loop was complete you would just return that best answer you had been keeping track of.Leibowitz
I
3

You can use this code to fix your problem, with this the algorithm don't need to go through 100 iterations to reach your answer (Is a modification of yours, the difference is in the for loop, when you use the current variable for the iteration):

 function sumFibs(num) {
  let previous = 0;
  let current = 1;
  let sum = 0;
  let next;

  for(current; current <= num;){
    next = current + previous;
    previous = current;

    if(current % 2 === 0) {
      sum += current;
    }

    current = next;
  }

  return sum;
}

sumFibs(4000000); // return 4613732
Irs answered 8/9, 2019 at 4:21 Comment(1)
Another form could be with a while loop and the same order for the algorithm.Irs
V
0

This could be one of the solutions in JavaScript

let a = 1;
let b = 2;
let upperBound = 4000000;
let c = a + b;
let sum = 2;

while(true) {
    c = a + b;
    if(c >= upperBound) {
        break;
    }
    if(c % 2 == 0){
        sum += c; 
        console.log(c);
    } 
    a = b;
    b = c;
}
console.log(`The Fibonacci for even numbers below ${upperBound} is:`, sum);
Vulture answered 5/5, 2022 at 21:35 Comment(0)
I
0
let fistNum = 0; //first number on the sequence
let secondNum = 1; //second number on the sequence
let nextNum; // next number on the sequence
let sum = 0;
let n = 4000000; //limit number


for( i=1; i<n; i++){
    nextNum = fistNum + secondNum;
    fistNum = secondNum ;
    secondNum = nextNum;
    if(secondNum%2===0 && secondNum<n){
        sum = sum + secondNum;
        console.log(sum);
    }
}
Isaak answered 18/9, 2022 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.