File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programming Diversions and the fly likes Euler problem #10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Euler problem #10" Watch "Euler problem #10" New topic
Author

Euler problem #10

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 865
    
  13

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?



"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Overflow?
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 865
    
  13

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: 4344
    
    8

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

Joined: Dec 16, 2010
Posts: 865
    
  13

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!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Euler problem #10
 
Similar Threads
Project Euler problem 6 algorithm
Project Euler problem 10
Can this be improved at all
Euler problem #1 - Spoiler Alert!
Project Euler Problem 25