Project Euler #5: Smallest multiple

  • + 0 comments
    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)));
    }
    }