0

the sum of even Fibonacci numbers below 4 mill : i am trying to do it using JavaScript, but i am getting infinity as an answer

but if i use small number such as 10, i am getting console.log() output with the result, is it possible to do that in JavaScript??

 var fib = [1, 2];

for(var i =fib.length; i<4000000; i++)
{
    fib[i] = fib[i-2] + fib[i-1];
}

 //console.log(fib);
 var arr_sum = 0;
for(var i = 0; i < fib.length; i++){
    if(fib[i] % 2 === 0){
        arr_sum += fib[i] ;
    }
}

console.log(arr_sum);
G. Santosh
  • 19
  • 7
  • You need to post the actual code involved. – Pointy Jul 20 '18 at 13:34
  • 1
    you're trying to sum the first 4 million fib(n) - you're only supposed to sum the ones that have a _value_ below 4e6. – Alnitak Jul 20 '18 at 13:48
  • ok, thanks for info – G. Santosh Jul 20 '18 at 14:17
  • You get infinity because at one point, the number gets very large that it exceeds the the range js can store. (1476th number as I noticed, to be exact1 = 3069892237633987e+308). Every number after this will be Infinity., hence summing them up will be Infinity too) – Nimeshka Srimal Jul 20 '18 at 17:34

2 Answers2

0

Two things before I get to coding:

  • You need to sum the even fibonacci numbers which VALUE below 4 Million (@Alnitak)

  • There is an "even fibonacci series" which can be calculated in a different manner, see here.

Alright here we go:

let fib = [0, 2];

for(let i = 2; fib[i-1] < 4000000; i++) {
    fib[i] = 4 * fib[i - 1] + fib[i - 2];
}
fib.pop()

let sum = fib.reduce((a, c) => a + c, 0);

console.log(sum);

Edit

Without the fib.pop() I just added, the last element of the array would be a number > 4000000. Now you should be able to get the right result.

Ahmed Bajra
  • 391
  • 1
  • 2
  • 14
  • HI, thanks for the answer, i ran the code and got the answer but the value that got from your code isn't matching with mine, please cross verify once again the code, i actually got 4613732 – G. Santosh Jul 20 '18 at 14:23
  • @G.Santosh I just verified it, thank you for the comment. – Ahmed Bajra Aug 13 '18 at 08:23
0

So this is the problem:

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.

The correct answer is 4613732.

On the script below, the answer would be from variable "sum". I assigned it an initial value of 2 since that is the first even number on our sequence. I used 3 more variables to traverse through this sequence. The loop runs normal Fibonacci sequence and the if statement filters even numbers and adds it to the sum.

I think this is the simplest and most intuitive code implementation.

var sum = 2;
var x = 1, y = 2, fib;

while(x < 4000000) {
   fib = x + y;
   x = y;
   y = fib;
   if(fib%2===0) sum += fib;
}

console.log(sum);
kennydelacruz
  • 114
  • 1
  • 4