aspose 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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Euler problem #10" Watch "Euler problem #10" New topic
Author

Euler problem #10

J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

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?



"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Overflow?
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

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: 4240
    
    7

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: 632
    
    7

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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Euler problem #10
 
Similar Threads
Project Euler problem 10
Euler problem #1 - Spoiler Alert!
Project Euler problem 6 algorithm
Project Euler Problem 25
Can this be improved at all