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?*/

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.

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.

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

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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.

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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…

In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …