Java program to find the smallest number divisible by each of the numbers 1 to 20.

This question is taken Project Euler. Link.
The question states:

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?

The logic behind this program is quite simple. The LCM(Least Common Multiple) of numbers from 1-20 give us the required answer.
We initialize the variable ‘num’ to 1 and i=2. We haven’t kept i=1 because finding LCM(1,1) is unnecessary and not required. So we start with num=LCM(num,i). The while loop keeps executing till i reaches its limit, which is 20 in this case.
For the logic behind the LCM function, please check this program. Link.

Code:

Output:
Output
The answer is: 232792560

Tested in Eclipse.

The following two tabs change content below.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">