Created
September 23, 2017 15:42
-
-
Save javamultiplex/b657de2dd6d8b7a5cf9c541d084f581a to your computer and use it in GitHub Desktop.
Project Euler Problem 5 : Smallest multiple from 1 to 20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment