• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Attempting a Project Euler question but getting an inexplicable ArithmeticException

 
Ranch Foreman
Posts: 44
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I recently started doing exercises on sites such as Code Chef and Project Euler, just for the fun of it. On the latter I was attempting Problem 3: What is the largest prime factor of the number 600851475143?

When calling the method below with the given number, it takes a really long time for an output to be generated. Ultimately, the code throws an ArithmeticException (division by zero) coming from line 7 according to the stack trace. But I don't understand why that is. Could someone help me out?





 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a problem that beginner programmers often have: They believe that their program does what they meant it to do. Then when an unexpected error message appears, it's hard to stop believing that.

Whereas as an outsider who didn't write the code, I say "Okay, at line 7 there's a division by zero. Well, line 7 doesn't do much except find something modulo i, so therefore i must be zero."

Well, okay, maybe you did get that far. (But you didn't say so.) So how does i get to be zero? That's the question to be asked.

You declared i to be an int value, and an int value can only contain numbers which are less than 4,294,967,296. You can count to that number by adding 1, and when you get there the count rolls over and goes to -4,294,967,296. If you carry on adding 1 for a while longer then the count eventually gets back to zero. And then your program crashes.

Notice that the number you used as input is about 600 billion, whereas an int value can only hold numbers up to about 4 billion. You could change your loop to use a double value rather than an int value, and then your program would probably not crash. But it would take about 100 times as long to run as your posted version did before it crashed. You may end up with a solution to Problem 3 but there must be faster ways.
 
Brecht Geeraerts
Ranch Foreman
Posts: 44
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the insight, Paul. It is much appreciated. I am familiar with the concept of overflow, but i failed to make the link. I did not understand why i could ever be zero, but now I do.

It is indeed so that once you believe your code is performing a certain task, it is not easy to "re-evaluate" your code in a critical way. I also did not know how to start debugging it - given the large value of the input number. I did test it with a smaller number and then the code did not fall apart.

I am sure that the algorithm could be optimised in more ways than one, I am indeed a beginner.  

Thanks again!!

 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:It is indeed so that once you believe your code is performing a certain thing, it is not easy to "re-evaluate" your code in a critical way.



Yes, it happened to me yesterday. I had several unlikely theories about the problem until finally I read the documentation and found out that I just used the wrong method.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!