import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* 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?
*/
public class Problem5 {
Map<Integer, Integer> factorize(int x) {
Map<Integer, Integer> factorMap = new HashMap<>();
for (int i = 2; i <= x; i++) {
if (x % i == 0) {
if (factorMap.containsKey(i)) {
factorMap.put(i, factorMap.get(i) + 1);
} else {
factorMap.put(i, 1);
}
x /= i;
i = 1;
}
}
return factorMap;
}
int solve(int x) {
int result = 1;
Map<Integer, Integer> minDivisible = new HashMap<>();
for (int i = 2; i <= x; i++) {
Map<Integer, Integer> factor = new HashMap<>();
factor = factorize(i);
for (int j : factor.keySet()) {
if (minDivisible.containsKey(j) && factor.get(j) > minDivisible.get(j)) {
minDivisible.replace(j, factor.get(j));
} else if (!minDivisible.containsKey(j)) {
minDivisible.put(j, factor.get(j));
}
}
}
for (int k : minDivisible.keySet()) {
result *= Math.pow(k, minDivisible.get(k));
}
return result;
}
public static void main(String[] args) {
// 232792560
System.out.println(new Problem5().solve(20));
}
}
정말 멍청하게 품 .......한심하네