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.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++){
int n = in.nextInt();
int ans = smallestDivisibleNumber(n);
System.out.println(ans);
}
}
private static int smallestDivisibleNumber(int n) {
int ans = 1;
for (int i = 2; i <= n; i++) {
ans = lcm(ans, i);
}
return ans;
}
private static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
Project Euler #5: Smallest multiple
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}