- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
42 lines (31 loc) · 921 Bytes
/
Copy pathMain.java
File metadata and controls
42 lines (31 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
importjava.math.BigInteger;
/*
@author: mc-es
Problem 5
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?
Answer : 232792560
*/
publicclassMain {
publicstaticvoidmain(String[] args) {
System.out.println(lcmOfRange(20));
}
publicstaticBigIntegergcd(BigIntegera, BigIntegerb) {
while (!b.equals(BigInteger.valueOf(0))) {
BigIntegertemp = b;
b = a.mod(b);
a = temp;
}
returna;
}
publicstaticBigIntegerlcm(BigIntegera, BigIntegerb) {
returna.multiply(b).divide(gcd(a, b));
}
publicstaticBigIntegerlcmOfRange(intn) {
BigIntegerlcmVal = BigInteger.valueOf(1);
for (inti = 1; i <= n; i++) {
lcmVal = lcm(lcmVal, BigInteger.valueOf(i));
}
returnlcmVal;
}
}