Solution to Project Euler Problem 5: Smallest multiple - 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?
Updated: March 7, 2021 — Training Time: 1 Minute
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 5: Smallest multiple.
Difficulty: Intermediate.
Objective: 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?
Input: None.
Expected Output: 232792560.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
import java.math.BigInteger;
public class SikademyEulerSolution {
public String run() {
BigInteger allLcm = BigInteger.ONE;
for (int i = 1; i <= 20; i++)
allLcm = lcm(BigInteger.valueOf(i), allLcm);
return allLcm.toString();
}
private static BigInteger lcm(BigInteger x, BigInteger y) {
return x.divide(x.gcd(y)).multiply(y);
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
import fractions
def compute():
ans = 1
for i in range(1, 21):
ans *= i // fractions.gcd(i, ans)
return str(ans)
if __name__ == "__main__":
print(compute())