programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Junilu Lacar
• Martin Vashko
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Scott Selikoff
• salvin francis
• Piet Souris

Euler problem #10

Bartender
Posts: 1810
28
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?

Bartender
Posts: 4568
9
Overflow?

J. Kevin Robbins
Bartender
Posts: 1810
28
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
Posts: 4568
9

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
Bartender
Posts: 1810
28
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!!

 Well behaved women rarely make history - Eleanor Roosevelt. tiny ad: Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java
Similar Threads