Solved

Project Euler Problem #5

Posted on 2014-02-12
10
276 Views
Last Modified: 2014-02-12
What is wrong with my code.  I can find numbers as long as divisor is lower than 19. When I try divisor = 19 or higher, I don't get anything returned:

public class SmallestMultiple{

     public static void main(String []args){

             int divisor=20;
             long i=1L;
             Boolean found = false;
             
             while(true)
             {
                 i++;
                 for(int j=divisor;j>0;j--)
                 {
                     if((i % j) == 0) 
                     {
                         if(j == 1) 
                         {
                             System.out.println(" " + i + " ");
                             found = true;
                             break;
                         }
                     }
                     else
                     {
                         break;
                     }  
                 }
                 
                 if(found)
                 {
                     System.out.println("Found\n");
                     break;
                 }
             }
    }
}
/*
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
*/

Open in new window

0
Comment
Question by:pzozulka
  • 4
  • 3
  • 3
10 Comments
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
You're breaking after the first iteration of the loop, no matter what. Take out this block and see how it behaves:

                     else
                     {
                         break;
                     }  

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
When I run it for divisor=20, I get
 232792560
Found

But counting up to 232792560 may be slow.
You may want to think about a more efficient way to do it.
 divisor=23, divisor=25, divisor=27, divisor=29,  divisor=31,  divisor=32, etc. would be even slower with this method.
0
 
LVL 8

Author Comment

by:pzozulka
Comment Utility
CPColin: That block of code is there to break the loop if I cannot evenly divide by any of the numbers between 20 - 1 (counting down). So I do intentionally want to break out of the loop if I cannot evenly divide.

Ozo: Perhaps I wasn't getting an answer because I was using an online java compiler that has a timeout set: http://www.compileonline.com/compile_java_online.php

I'm just happy the logic is correct. I'm sure there are an infinite many other ways to do this -- some more efficient -- and some less (based on what I've seen on stackoverflow).
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
I do intentionally want to break out of the loop if I cannot evenly divide.

The code, as you have it right now, breaks out of the loop as soon as the first number fails to divide evenly. Is that what you want?
0
 
LVL 8

Author Comment

by:pzozulka
Comment Utility
Yes, I think so because what's the point of continuing to attempt to divide by any other numbers between 1 - 20 if one of them already failed.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
Comment Utility
Assuming that you wanted to break out of the loop as soon as the first number fails to divide evenly, which would be consistent with a result of 2520 for d=10,
you might want to look at when and by how much the result increases as you increase d.
If you notice any patterns, that may give you some clues to ways to do it that would be less prone to timeouts.
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
Looks like I misunderstood what the code is trying to do. I'll bow out.
0
 
LVL 8

Author Comment

by:pzozulka
Comment Utility
Hmmm...that makes sense. Am I on the right track to assume to iterate i by approximately "increase amount" rather than "i++"?
0
 
LVL 8

Author Comment

by:pzozulka
Comment Utility
Thanks for your help CPColin.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
If you can find an appropriate "increase amount", that may be one way to gain efficiency.
A smaller "increase amount" may take longer, but too large an "increase amount" may risk skipping past the smallest solution.
If you can see any relationship between the results for different divisor values, it may give you some hints about how some possible "increase amount" may work.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now