We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
System.out.println(lcm(n));
}
sc.close();
}
private static BigInteger lcm(int n) {
BigInteger lcm = BigInteger.valueOf(1);
for (int i = 2; i <= n; i++) {
lcm = lcm(lcm, i);
}
return lcm;
}
private static BigInteger lcm(BigInteger a, int b) {
return a.multiply(BigInteger.valueOf(b))
.divide(a.gcd(BigInteger.valueOf(b)));
}
}
Project Euler #5: Smallest multiple
You are viewing a single comment's thread. Return to all comments →