Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Project Euler Problem 1
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: Sat Mar 30, 2013 7:34 pm   Post subject: Project Euler Problem 1

WRONG TITLE, MY BAD. It's problem 2.

c++:

#include <iostream>
using namespace std;

int main()
{
    int fib1=1, fib2=1, fibSum, total;
    while (fibSum < 4000000)
    {
        fibSum = fib1 + fib2;
        if (fibSum % 2 == 0)
            total += fibSum;
        fib1 = fib2;
        fib2 = fibSum;
    }
    if (total > 4000000)
        total -= fibSum;
    cout << total;
}

That's how I made my code. The answer is 1963521375 when I ran the program though, not even close to how it's supposed to be. I can't think of a way to fix this, and when I change some things around, I get the exact same answer. I don't even know what's going on, can somebody help me please?

Here's the question:

Quote:

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.


I know, I know, I'm really bad at this Sad
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Mar 30, 2013 9:52 pm   Post subject: RE:Project Euler Problem 1

What are the initial values of fibSum and total?
Panphobia




PostPosted: Sat Mar 30, 2013 10:46 pm   Post subject: Re: Project Euler Problem 1

get rid of the
code:
if (total > 4000000)
        total -= fibSum;
you don't need it, other than that, Insectoid is right, look at total and fibSum
Dreadnought




PostPosted: Sat Mar 30, 2013 11:45 pm   Post subject: Re: Project Euler Problem 1

Panphobia wrote:

get rid of the
code:
if (total > 4000000)
        total -= fibSum;
you don't need it

While it is true that this will cause a problem, an if statement similar to this one is needed. There's just one little mistake in the statement. (wrong identifier possibly?)
Panphobia




PostPosted: Sat Mar 30, 2013 11:57 pm   Post subject: Re: Project Euler Problem 1

Dreadnought @ Sat Mar 30, 2013 11:45 pm wrote:
Panphobia wrote:

get rid of the
code:
if (total > 4000000)
        total -= fibSum;
you don't need it

While it is true that this will cause a problem, an if statement similar to this one is needed. There's just one little mistake in the statement. (wrong identifier possibly?)
the question only puts a restriction on the maximum value of the fibonacci number you are allowed to add to the total, not the total itself, so this would work
Cancer Sol




PostPosted: Sun Mar 31, 2013 4:33 pm   Post subject: Re: Project Euler Problem 1

Panphobia @ 3/30/2013, 11:57 pm wrote:
Dreadnought @ Sat Mar 30, 2013 11:45 pm wrote:
Panphobia wrote:

get rid of the
code:
if (total > 4000000)
        total -= fibSum;
you don't need it

While it is true that this will cause a problem, an if statement similar to this one is needed. There's just one little mistake in the statement. (wrong identifier possibly?)
the question only puts a restriction on the maximum value of the fibonacci number you are allowed to add to the total, not the total itself, so this would work


Oh... I read the question a million times and I thought i meant the total xD
My friend made one in turing though, got the same answer as I did before I added the if statement at the end, and it was wrong still.
Cancer Sol




PostPosted: Sun Mar 31, 2013 6:49 pm   Post subject: Re: Project Euler Problem 1

Still doesn't work :/
Panphobia




PostPosted: Sun Mar 31, 2013 9:12 pm   Post subject: RE:Project Euler Problem 1

post your updated code Smile
Sponsor
Sponsor
Sponsor
sponsor
Cancer Sol




PostPosted: Mon Apr 01, 2013 9:35 am   Post subject: Re: RE:Project Euler Problem 1

Panphobia @ 3/31/2013, 9:12 pm wrote:
post your updated code Smile

c++:

#include <iostream>
using namespace std;

bool checkEven(int number)
{
    if (number % 2 == 0)
        return true;
    else
        return false;
}

struct sequence
{
    int one, two, sum, total;
};

int main()
{
    sequence fib;
    fib.one=1, fib.two=1, fib.total=0;
    while (fib.total < 4000000)
    {
        fib.sum = fib.one + fib.two;
        if (checkEven(fib.sum) == true)
            fib.total += (fib.one + fib.two);
        fib.one = fib.two;
        fib.two = fib.sum;
    }
    cout << fib.total;
}

edit: I just changed the if statement that checks if the content is even from fib.one to fib.sum; I have no idea why fib.one was there
edit 2: It's still wrong
nullptr




PostPosted: Mon Apr 01, 2013 9:44 am   Post subject: Re: Project Euler Problem 1

Don't check if the total is greater than four million -- check if the current term is greater than four million.
Cancer Sol




PostPosted: Mon Apr 01, 2013 10:00 am   Post subject: Re: Project Euler Problem 1

nullptr @ 4/1/2013, 9:44 am wrote:
Don't check if the total is greater than four million -- check if the current term is greater than four million.

When I changed the while-loop to check fib.two, I got the same answer still.
nullptr




PostPosted: Mon Apr 01, 2013 10:41 am   Post subject: Re: Project Euler Problem 1

Cancer Sol @ Mon Apr 01, 2013 10:00 am wrote:
nullptr @ 4/1/2013, 9:44 am wrote:
Don't check if the total is greater than four million -- check if the current term is greater than four million.

When I changed the while-loop to check fib.two, I got the same answer still.


I just copy-pasted your code and it gives the right answer (with or without that change). Try entering it in again -- maybe you made a typo the first time?
Cancer Sol




PostPosted: Mon Apr 01, 2013 11:42 am   Post subject: Re: Project Euler Problem 1

nullptr @ 4/1/2013, 10:41 am wrote:
Cancer Sol @ Mon Apr 01, 2013 10:00 am wrote:
nullptr @ 4/1/2013, 9:44 am wrote:
Don't check if the total is greater than four million -- check if the current term is greater than four million.

When I changed the while-loop to check fib.two, I got the same answer still.


I just copy-pasted your code and it gives the right answer (with or without that change). Try entering it in again -- maybe you made a typo the first time?


The first time I made the program for it, I had the answer you just got too, but every time I enter it, it's wrong.
Maybe I'm just bad at copying it over to projecteuler.net lol
nullptr




PostPosted: Mon Apr 01, 2013 11:52 am   Post subject: Re: Project Euler Problem 1

Cancer Sol @ Mon Apr 01, 2013 11:42 am wrote:
nullptr @ 4/1/2013, 10:41 am wrote:
Cancer Sol @ Mon Apr 01, 2013 10:00 am wrote:
nullptr @ 4/1/2013, 9:44 am wrote:
Don't check if the total is greater than four million -- check if the current term is greater than four million.

When I changed the while-loop to check fib.two, I got the same answer still.


I just copy-pasted your code and it gives the right answer (with or without that change). Try entering it in again -- maybe you made a typo the first time?


The first time I made the program for it, I had the answer you just got too, but every time I enter it, it's wrong.
Maybe I'm just bad at copying it over to projecteuler.net lol


Are you getting 4613732 as your answer?
Cancer Sol




PostPosted: Mon Apr 01, 2013 12:00 pm   Post subject: Re: Project Euler Problem 1

nullptr @ 4/1/2013, 11:52 am wrote:
Cancer Sol @ Mon Apr 01, 2013 11:42 am wrote:
nullptr @ 4/1/2013, 10:41 am wrote:
Cancer Sol @ Mon Apr 01, 2013 10:00 am wrote:
nullptr @ 4/1/2013, 9:44 am wrote:
Don't check if the total is greater than four million -- check if the current term is greater than four million.

When I changed the while-loop to check fib.two, I got the same answer still.


I just copy-pasted your code and it gives the right answer (with or without that change). Try entering it in again -- maybe you made a typo the first time?


The first time I made the program for it, I had the answer you just got too, but every time I enter it, it's wrong.
Maybe I'm just bad at copying it over to projecteuler.net lol


Are you getting 4613732 as your answer?

Yes, but projecteuler keeps saying it's incorrect Sad
I'll just try a few more times again.

Edit: Nvm, the answer I have is 4285822
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 28 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: