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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #5: Smallest multiple
  4. Discussions

Project Euler #5: Smallest multiple

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • vipin_katiyar
    4 months ago+ 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)));
    }
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy