ArithmeticUtils { public static int lcm(int a, int b) throws MathArithmeticException { if (a == 0 || b == 0){ return 0; } int lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_32_BITS, a, b); } return lcm; } private  ArithmeticUtils(); static int addAndCheck(int x, int y); static long addAndCheck(long a, long b); @Deprecated static long binomialCoefficient(final int n, final int k); @Deprecated static double binomialCoefficientDouble(final int n, final int k); @Deprecated static double binomialCoefficientLog(final int n, final int k); @Deprecated static long factorial(final int n); @Deprecated static double factorialDouble(final int n); @Deprecated static double factorialLog(final int n); static int gcd(int p, int q); static long gcd(final long p, final long q); static int lcm(int a, int b); static long lcm(long a, long b); static int mulAndCheck(int x, int y); static long mulAndCheck(long a, long b); static int subAndCheck(int x, int y); static long subAndCheck(long a, long b); static int pow(final int k,
                          final int e); @Deprecated static int pow(final int k, long e); static long pow(final long k,
                           final int e); @Deprecated static long pow(final long k, long e); static BigInteger pow(final BigInteger k, int e); static BigInteger pow(final BigInteger k, long e); static BigInteger pow(final BigInteger k, BigInteger e); @Deprecated static long stirlingS2(final int n, final int k); static boolean isPowerOfTwo(long n);  }
@Test public void testLcm() { int a = 30; int b = 50; int c = 77; Assert.assertEquals(0, ArithmeticUtils.lcm(0, b)); Assert.assertEquals(0, ArithmeticUtils.lcm(a, 0)); Assert.assertEquals(b, ArithmeticUtils.lcm(1, b)); Assert.assertEquals(a, ArithmeticUtils.lcm(a, 1)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, -b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, -b)); Assert.assertEquals(2310, ArithmeticUtils.lcm(a, c)); Assert.assertEquals((1<<20)*15, ArithmeticUtils.lcm((1<<20)*3, (1<<20)*5)); Assert.assertEquals(0, ArithmeticUtils.lcm(0, 0)); try { ArithmeticUtils.lcm(Integer.MIN_VALUE, 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { } try { ArithmeticUtils.lcm(Integer.MIN_VALUE, 1<<20); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { } try { ArithmeticUtils.lcm(Integer.MAX_VALUE, Integer.MAX_VALUE - 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { } }
@Test public void testLcmLong() { long a = 30; long b = 50; long c = 77; Assert.assertEquals(0, ArithmeticUtils.lcm(0, b)); Assert.assertEquals(0, ArithmeticUtils.lcm(a, 0)); Assert.assertEquals(b, ArithmeticUtils.lcm(1, b)); Assert.assertEquals(a, ArithmeticUtils.lcm(a, 1)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, -b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, -b)); Assert.assertEquals(2310, ArithmeticUtils.lcm(a, c)); Assert.assertEquals(Long.MAX_VALUE, ArithmeticUtils.lcm(60247241209L, 153092023L)); Assert.assertEquals((1L<<50)*15, ArithmeticUtils.lcm((1L<<45)*3, (1L<<50)*5)); Assert.assertEquals(0L, ArithmeticUtils.lcm(0L, 0L)); try { ArithmeticUtils.lcm(Long.MIN_VALUE, 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { } try { ArithmeticUtils.lcm(Long.MIN_VALUE, 1<<20); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { } Assert.assertEquals((long) Integer.MAX_VALUE * (Integer.MAX_VALUE - 1), ArithmeticUtils.lcm((long)Integer.MAX_VALUE, Integer.MAX_VALUE - 1)); try { ArithmeticUtils.lcm(Long.MAX_VALUE, Long.MAX_VALUE - 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { } }