Computer Science Canada

Project Euler Problem 1

Author:  Cancer Sol [ 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

Author:  Insectoid [ Sat Mar 30, 2013 9:52 pm ]
Post subject:  RE:Project Euler Problem 1

What are the initial values of fibSum and total?

Author:  Panphobia [ 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

Author:  Dreadnought [ 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?)

Author:  Panphobia [ 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

Author:  Cancer Sol [ 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.

Author:  Cancer Sol [ Sun Mar 31, 2013 6:49 pm ]
Post subject:  Re: Project Euler Problem 1

Still doesn't work :/

Author:  Panphobia [ Sun Mar 31, 2013 9:12 pm ]
Post subject:  RE:Project Euler Problem 1

post your updated code Smile

Author:  Cancer Sol [ 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

Author:  nullptr [ 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.

Author:  Cancer Sol [ 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.

Author:  nullptr [ 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?

Author:  Cancer Sol [ 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

Author:  nullptr [ 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?

Author:  Cancer Sol [ 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

Author:  nullptr [ Mon Apr 01, 2013 1:07 pm ]
Post subject:  Re: Project Euler Problem 1

Cancer Sol @ Mon Apr 01, 2013 12:00 pm wrote:
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


That's really weird, it works on codepad: http://codepad.org/vqvwKwvs. It must be something on your computer or compiler.

Author:  Cancer Sol [ Tue Apr 02, 2013 12:56 am ]
Post subject:  Re: Project Euler Problem 1

nullptr @ 4/1/2013, 1:07 pm wrote:
Cancer Sol @ Mon Apr 01, 2013 12:00 pm wrote:
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


That's really weird, it works on codepad: http://codepad.org/vqvwKwvs. It must be something on your computer or compiler.


I'll try it on compilr to see if it's fine on it. if it is, it might be my computer (actually, it's my mom's)

Author:  Insectoid [ Tue Apr 02, 2013 6:45 am ]
Post subject:  RE:Project Euler Problem 1

code:
fib.one=1, fib.two=1, fib.total=0;


There's something missing from this line.

Author:  Cancer Sol [ Tue Apr 02, 2013 10:48 am ]
Post subject:  Re: RE:Project Euler Problem 1

Insectoid @ 4/2/2013, 6:45 am wrote:
code:
fib.one=1, fib.two=1, fib.total=0;


There's something missing from this line.

Am I supposed to assign fib.sum its value already? I'm not sure what I'm missing still.
So do I do it like:
code:

fib.one=1, fib.two=1, fib.sum=fib.one + fib.two, fib.total=0;

Author:  Insectoid [ Tue Apr 02, 2013 5:13 pm ]
Post subject:  RE:Project Euler Problem 1

Woops, never mind. Thought you were adding to it instead of assigning it directly.

Author:  Cancer Sol [ Thu Apr 04, 2013 3:45 pm ]
Post subject:  Re: Project Euler Problem 1

Idk, I give up. Too hard lol xD
I'll do these questions another time.
The only one I could do is only the first question, so sad Crying or Very sad

Author:  Panphobia [ Thu Apr 04, 2013 4:58 pm ]
Post subject:  Re: Project Euler Problem 1

Ok I edited your code from when you first posted a while ago, and since you might be having problems with your compiler, try this, this gave me the correct answer 100%
c++:
#include <iostream>
using namespace std;

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

Author:  Cancer Sol [ Fri Apr 05, 2013 1:17 am ]
Post subject:  Re: Project Euler Problem 1

The answer is 4613732 right? Because when I entered it in the website, it still said it's wrong.

Author:  nullptr [ Fri Apr 05, 2013 7:21 am ]
Post subject:  Re: Project Euler Problem 1

Cancer Sol @ Fri Apr 05, 2013 1:17 am wrote:
The answer is 4613732 right? Because when I entered it in the website, it still said it's wrong.


Yeah, it is 4613732. It even says so on the problem page once you've solved the problem, so I can't think what's giving you trouble.

Author:  Panphobia [ Fri Apr 05, 2013 8:30 am ]
Post subject:  RE:Project Euler Problem 1

Cancer maybe you have entered the security code wrong? Have you ever thought of that?

Author:  Cancer Sol [ Fri Apr 05, 2013 4:17 pm ]
Post subject:  Re: RE:Project Euler Problem 1

Panphobia @ 4/5/2013, 8:30 am wrote:
Cancer maybe you have entered the security code wrong? Have you ever thought of that?

Well here's what the second page says after I enter the answer:
Quote:
Sorry, but the answer you gave appears to be incorrect.

I don't think that's the wrong confirmation code :/

Author:  Panphobia [ Fri Apr 05, 2013 4:43 pm ]
Post subject:  RE:Project Euler Problem 1

definitely is the right answer

MOD Edit: Let's draw the line at helping with the code. We don't need to see the actual value.

Author:  Cancer Sol [ Fri Apr 05, 2013 4:51 pm ]
Post subject:  Re: RE:Project Euler Problem 1

Oh wait actually... I've been entering it for the wrong question, my bad xD
Thanks! I was entering it for question 3 the whole time, LOL.
That was why it didn't work the whole time...


: