| Author |
Euler problem #10
|
J. Kevin Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 385
|
|
Another spoiler. This code works for values of 10 and 100, but according to the Euler site, I'm getting the wrong sum for the value of 2000000.
I've spent hours looking at this and can't figure out why it's inconsistent. Can anyone spot the problem?
|
"I have a mind like a steel... uh... thingy."
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
|
Overflow?
|
 |
J. Kevin Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 385
|
|
Nope, the answer I get is 1,179,908,154. An int will hold up to 2,147,483,647.
I'm thinking maybe the square root cast to an int is causing a rounding error which might cause it to falsely identify a prime or two. My next step is to remove the use of the square root and just test all possible divisors.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
Jk Robbins wrote:Nope, the answer I get is 1,179,908,154. An int will hold up to 2,147,483,647.
Yes, but what if it's already gone all the way around? You're storing the answer in an int - of course it's going to look like it's within range! Just switch to a long, see what happens.
|
 |
J. Kevin Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 385
|
|
Doh! Got it!
It didn't even occur to me that it might have wrapped around. I thought it would throw an error if I tried to exceed the max value.
Live and learn. That's why we do these exercises, right?
Thanks!!
|
 |
 |
|
|
subject: Euler problem #10
|
|
|