# Project Euler | Problem 5 | Smallest Multiple

##
__Problem Description__ :

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?

##
__Concept__ :

Do you know how we got 2520? Answer is very simple if we will try to get LCM(1,2,3,4,5,6,7,8,9,10) then it is 2520. It means we have to find LCM of all the number from 1 to 20. Now question is how to get LCM of two number in Java? So check below algorithm.

##
__Algorithm__ :

We have to perform following steps for finding LCM of two numbers.

- Get HCM of two numbers because LCM * HCM = product of two numbers. If we will get HCM then we can easily find LCM also.
- By Euclidean algorithm we can find HCM.
- We got HCM, so LCM = (product of two numbers) / HCM.

##
__Java Program__ :

package com.javamultiplex.projecteuler; /** * * @author Rohit Agarwal * @category Project Euler Problems * @problem Smallest multiple from 1 to 20 * */ public class Problem5 { public static void main(String[] args) { long limit = 20; long result = 1; for (int i = 2; i <= limit; i++) { result = getLCM(result, i); } System.out.println("The smallest positive number that is evenly divisible by " + "all of the numbers from 1 to 20 is : " + result); } private static long getLCM(long result, int i) { long HCM = getHCM(result, i); // HCM is must for LCM long LCM = (result * i) / HCM; // relationship between HCM and LCM is // HCM*LCM=number1*number2 return LCM; } /* * Here we are using Euclidean algorithm for find HCM of two numbers. * */ private static long getHCM(long result, int i) { long a, b, temp; if (result > i) { a = result; b = i; } else { a = i; b = result; } while (b != 0) { temp = b; b = a % b; a = temp; } return a; } }

##
__Output__ :

##
__References__ :

https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm

http://www.math.com/school/subject1/lessons/S1U3L3DP.html

Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

**Tags : Project Euler Problem 5, Solution in Java, Smallest Multiple, LCM, HCM, if else statement, while loop, for loop.**

Project Euler | Problem 5 | Smallest Multiple
Reviewed by Rohit Agarwal
on
12/17/2016
Rating:

## No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.