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

In summary, The attempt at a solution does not work for really big numbers. 4000000th Fibonacci number is 5.404785360620536653971914908.
  • #1
newfeeling
2
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;

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


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

num1 = num2;
num2 = fibnum;

}

cout << 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
  • #2
What do you mean by "doesn't work"? What's happening?

Have you checked how large Fibonacci numbers get?
 
Last edited:
  • #3
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.
 
  • #4
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?
 
  • #5
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;

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


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

num1 = num2;
num2 = fibnum;

}

cout << 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:
  • #6
ups...
 
  • #7
Code:
s=0
for n=0 ... 4000000
    if is.even( fibonacci(n) )
        s += fibonacci(n)
print s
[tex]\approx 5.404785360620536653971914908\cdot 10 ^{1204119}[/tex]
 

1. What is the Fibonacci sequence?

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The first few numbers in the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

2. What is the purpose of Project Euler Problem 002?

The purpose of Project Euler Problem 002 is to challenge programmers to find the sum of all even-valued terms in the Fibonacci sequence that are below a given limit. This problem helps programmers practice their problem-solving skills and improve their understanding of programming concepts.

3. How do I approach solving this problem in C++?

To solve this problem in C++, you can use a loop to generate the Fibonacci sequence and check if each term is even. If it is, add it to a running sum. You can also use the modulus operator to check if a number is even or odd. Remember to set a limit to avoid an infinite loop.

4. What is the time complexity of the solution to Project Euler Problem 002 in C++?

The time complexity of the solution to Project Euler Problem 002 in C++ is O(n), where n is the limit given. This is because the algorithm only needs to iterate through the Fibonacci sequence once to find the sum of all even-valued terms below the limit.

5. Can I use a different programming language to solve Project Euler Problem 002?

Yes, you can use any programming language to solve Project Euler Problem 002. However, the syntax and approach may vary depending on the language. The key is to understand the problem and use the appropriate logic and tools in your chosen language to solve it efficiently.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top