Project Euler Problem 002: Fibonacci sequence (in C++)

Click For Summary

Discussion Overview

The discussion revolves around a programming challenge from Project Euler, specifically Problem 002, which involves calculating the sum of even-valued terms in the Fibonacci sequence that do not exceed four million. Participants are sharing their code attempts in C++ and discussing issues related to integer overflow and the correct interpretation of the problem statement.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their C++ code and notes it works for small limits but fails for larger ones, indicating a flaw in their algorithm.
  • Another participant questions what "doesn't work" means and prompts for clarification on the results obtained with large inputs.
  • A later reply mentions receiving incorrect results, such as negative numbers, when using large inputs, suggesting integer overflow as a potential issue.
  • One participant points out the need to consider the size of integers used in the program, questioning whether 32 or 64-bit integers are sufficient for storing large Fibonacci numbers.
  • Another participant clarifies the problem's requirement, emphasizing the need to sum Fibonacci numbers that do not exceed four million, rather than summing the first four million terms of the sequence.
  • Suggestions are made to use a while loop instead of a for loop to handle the unknown number of iterations required for the sum.
  • One participant provides links to Wolfram Alpha to illustrate the size of Fibonacci numbers and the limitations of finite-sized integers in the context of the problem.
  • A final post presents a pseudocode approach to calculating the sum, indicating a very large result, which further highlights the challenges of handling large Fibonacci numbers.

Areas of Agreement / Disagreement

Participants express differing views on the interpretation of the problem statement and the approach to coding the solution. There is no consensus on the best method to handle large Fibonacci numbers or the specific implementation details.

Contextual Notes

There are unresolved issues regarding the limitations of integer sizes in C++ and the implications of overflow when calculating large Fibonacci numbers. The discussion also highlights the need for clarity in problem interpretation, particularly regarding the summation criteria.

newfeeling
Messages
2
Reaction score
0

Homework Statement


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.

Homework Equations


n/a

The Attempt at a Solution


#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{

int n;
int fibnum;
int sum;
int num2;
int num1;
int limit;

num2 = 1;
num1 = 0;
fibnum = 0;
sum = 0;

count << "n?" << endl;
cin >> limit;


for(n = 0; n < limit; n += 1)
{
fibnum = num2 + num1;
if(fibnum % 2 == 0)
{
sum += fibnum;
}

num1 = num2;
num2 = fibnum;

}

count << sum << endl;

return EXIT_SUCCESS;

}This chunk of code allows me to get the correct sum when "limit" is a relatively small number (e.g. 10 will return 44 and 5 will return 10), but doesn't work for really big numbers...like 4,000,000. I'm not looking for a solution; I know that if my algorithm were designed correctly I would have already gotten the correct answer, but clearly there is some glaring flaw that I'm not noticing. Am I on the right track with this method at least, or am I wasting my time beating my head over my desk with this approach?
 
Physics news on Phys.org
What do you mean by "doesn't work"? What's happening?

Have you checked how large Fibonacci numbers get?
 
Last edited:
Borek said:
What do you mean by "doesn't work"? What's happening?

Have you checked how large Fibonacci numbers get?

When I punch in sufficiently large number for the program, I get clearly bogus results like -98123786458 for example; this problem does not arise with numbers that are not particularly big. And yes I have checked how large Fibonacci numbers get O_O.
 
You are using either a 32 or 64 bit integers, are they sufficiently large to store Fibonacci numbers? How many bits needed to store 4000000th Fibonacci number?
 
newfeeling said:

Homework Statement


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.

Homework Equations


n/a

The Attempt at a Solution


#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{

int n;
int fibnum;
int sum;
int num2;
int num1;
int limit;

num2 = 1;
num1 = 0;
fibnum = 0;
sum = 0;

count << "n?" << endl;
cin >> limit;


for(n = 0; n < limit; n += 1)
{
fibnum = num2 + num1;
if(fibnum % 2 == 0)
{
sum += fibnum;
}

num1 = num2;
num2 = fibnum;

}

count << sum << endl;

return EXIT_SUCCESS;

}This chunk of code allows me to get the correct sum when "limit" is a relatively small number (e.g. 10 will return 44 and 5 will return 10), but doesn't work for really big numbers...like 4,000,000. I'm not looking for a solution; I know that if my algorithm were designed correctly I would have already gotten the correct answer, but clearly there is some glaring flaw that I'm not noticing. Am I on the right track with this method at least, or am I wasting my time beating my head over my desk with this approach?
It asks for the sum of every part of the sequence that does not exceed 4 million. It doesn't ask for the sum of the first 4 million in the sequence.

For example, if I had a sequence
s_n = 2^n, n from 0 to infinity

then the sum of the first 5 would be
1 + 2 + 4 + 8 + 16
whereas the sum of all values that do not exceed five in the sequence would be
1 + 2 + 4

Do you see the difference?

In this circumstance, you should probably look into doing a while loop since you do not know exactly how many iterations you will need to do.

On a side note, you can initialize a value to a variable when you declare it, e.g.
int num2 = 1;

And to answer the question of why your code fails with such an insane limit, take a look at this:
http://www.wolframalpha.com/input/?i=fibonacci[11%2C1]
So you plug in n+1 to see what the nth fib is. Let's plug in 100...
http://www.wolframalpha.com/input/?i=fibonacci[101%2C1]
The answer is 573147844013817084101

Your finite-sized integers are overflowing. They simply cannot hold a number that large.

Let's see what Wolfram says 4,000,000 is:
http://www.wolframalpha.com/input/?i=fibonacci[4000001%2C1]
2.63331342366565668198735005895985564737121509 × 10^835950
 
Last edited:
ups...
 
Code:
s=0
for n=0 ... 4000000
    if is.even( fibonacci(n) )
        s += fibonacci(n)
print s
\approx 5.404785360620536653971914908\cdot 10 ^{1204119}
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
11
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K