# Sum of Fibonacci numbers

This problem is very buggy. The tests listed make no sense. It’s too much to elaborate, but anyone who just looks at it will see what the problem is. I can’t believe such errors are still sitting around unfixed.

HI @schachmatte !

Well, this is the place to elaborate. The only way these things get fixing is if we have discussions about them and bring up an issue on github.
Otherwise nothing changes.

1. Two of the tests say " `sumFibs(75024)` should return 60696." and also “sumFibs(75025) should return 135721.” Impossible.

2. “sumFibs(4000000) should return 4613732.” This answer is correct for the even Fibonacci numbers smaller than 4000000, but the problem asks for the sum of the odd ones.

I doubled checked with a new solution I wrote and everything seems to check out.

I suspect you may have an error in how you compute the Fibbonacci numbers, how you check even/odd, or how you check your termination condition. If you post your code, we can help you find the bug in your code.

How is that impossible,

`sumFibs(75024) = 60696`,
`sumFibs(75025) = 60696 + 75025 = 135721`,

do you disagree?

In fact `sumOfOddFibs(4000000) === sumOfEvenFibs(4000000)`

1 Like

sumFibs(75024) = 60696 if you add the even FIbonaccis <= 75024. If you add the odd Fibonaccis, then sumFibs(75024) = 135721. So it seems like for 75024 you are summing the evens and for 75025 you are summing the odds.

So if you are doing odd FIbonaccis, then sum(75025) = 257114, since 75025 is itself an odd Fibonacci number.

How can this be? Why would you expect this if there are twice as many odd Fibonaccis as evens? I get sumOfOddFibs(4000000) = 10316619 and sumOfEvenFibs(4000000) = 4613732.

Please share your code so we can figure out what is wrong with it.

I’d like for jsdisco to explain his/her reasoning first.

Your code is flawed. Trying to explain what is wrong when you are using wrong results for your reasoning simply won’t work. Again, please share your code so we can show your where your bug is. It sounds like at the very least you have a bug in your even/odd checking.

You think it is a coincidence that I passed all the tests? Also, I solved this Project Euler problem correctly, which is sumFibs(4000000)., when the FIbs you are summing are even.

It might seem counter-intuitive that the sumOfOdds is (often) equal to the sumOfEvens, and a mathematician might be able to explain it better than me, but just try it out with smaller sequences.

``````sumOfOdds(10) = 10;
sumOfEvens(10) = 10;

sumOfOdds(50) = 44;
sumOfEvens(50) = 44;
``````

And I agree with Jeremy that it would help if you shared your code. One common pitfall in this challenge is that the last item of your Fib Array (assuming you’re building an array) is higher than `num`. I suspect something like that is your bug.

1 Like

I think you have a bug that you refuse to consider. Please share your code.

This cannot possibly be correct because the sum of every Fibbonacci that is less than or equal to 75024 is 121392.

The fact that the sum of the evens often equals the sum of the odds is a cool feature of how the Fibbonacci sequence is generated

``````1, 1, 2, 3, 5, 8, 13, 21, 34, ...
``````

The Fibbonacci numbers are the sum of the previous two entries, and naturally this results in the pattern

``````odd, odd, even, odd, odd, even, odd, odd, even...
``````

This means that every even is the sum of the preceding two odd values. Thus, every single even added together must equal every single odd added together if the last Fibbonacci number you are considering is even. (If not, then you are going to be off by a little amount in the sums)

Ah ha! I think I exactly duplicated their bug by doing something along these lines! I am seeing all the wrong values they are reporting. That’s some next level debugging right there - correctly debugging code OP refused to provide. Nice work!

1 Like

Another cool feature of the Fibonaccis is that the total sum of `N` numbers is equal to `Fib(N+2) - 1`. (in other words, the total sum of the first `10` numbers is the 12th number minus 1).

With that, you can easily check on a table of numbers (like List of Fibonacci numbers) that the total sum of all numbers `<= 75025` is `196417`. You have ` 257114` just for the odd numbers, so that’s obviously too high.

You can also see that for numbers `<=4000000`, the total sum is `9227464`. Divide that by `2` and you get `4613732`, which is the correct result for both the odd and the even sums.

1 Like

I’m psychic Or I just projected my own mistake on them … (redid the challenge too, and stumbled right into that trap)

2 Likes

First of all, I didn’t refuse to provide my code. I just wanted to hear an explanation for something I didn’t believe. However, I was wrong, so thank you to both of you for helping me find my mistake. Also thanks for the interesting insights regarding the Fibonacci numbers.

That being said, I’ll post my amateurish code, just in case you want to provide some feedback:

``````function sumFibs(num, parity) {
let first = 1;
let second = 1;
let sum = 0;
if (parity == 'odd') {
sum = 2;
}

while (first + second <= num) {
let third = first + second;
console.log(third);
if (third % 2 == 1) {
sum += third;
}
first = second;
second = third;

}
return sum;
}

console.log('sum: ' + sumFibs(75024, 'odd'));
``````

I don’t think that’s an amateurish solution, a loop is the best/cleanest way to solve this in my opinion, especially if you only store the values you need (“amateurish” would be building an array of Fibs, and then looping a second time to sum up the odd numbers). If you want to get fancy, maybe use recursion.

The fact that the odd and even sums are equal in 1/3 of cases was new to me, too, I’m surprised I never saw it mentioned anywhere, especially considering how often Fibonaccis appear in algorithm challenges.

1 Like

Thank you. I enjoyed this thread. My first programming assignment many many years ago was to generate first N Fibonacci numbers (in FORTRAN!). I’m always fascinated with Fibonacci numbers and golden ratio.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.