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 #1 - Spoiler Alert! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Euler problem #1 - Spoiler Alert!" Watch "Euler problem #1 - Spoiler Alert!" New topic
Author

Euler problem #1 - Spoiler Alert!

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 914
    
  13

I've finally signed up for the Euler project to improve my math and programming skills. I'm interested in feedback on my first solution. I'm not looking for the fastest solution or the least number of lines of code, but rather using sound coding practices that can be applied to any problem.

Now, how could I go about improving this code?


"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: 4363
    
    8

One thing that springs to mind is that you're effectively using two variables, i and numberToTest, for one purpose (iterating across the required range). That's more error prone because you've got to keep them in sync. I'd suggest just using numberToTest and looping from 1 to limit.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 914
    
  13

Thanks, excellent advice. This is why I like to ask for a different perspective on things. New code:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Euler problem #1 - Spoiler Alert!