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.
  1. Get HCM of two numbers because LCM * HCM = product of two numbers. If we will get HCM then we can easily find LCM also.
  2. By Euclidean algorithm we can find HCM.
  3. 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 :

Output of Project Euler Problem 5 - Smallest Multiple in Java
Output - Project Euler Problem 5 

References :

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.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.

Let's Get Connected: Linkedin | Facebook |

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

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.

Powered by Blogger.