ArithmeticUtils { public static int pow(final int k, final int e) throws NotPositiveException, MathArithmeticException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } try { int exp = e; int result = 1; int k2p = k; while (true) { if ((exp & 0x1) != 0) { result = mulAndCheck(result, k2p); } exp >>= 1; if (exp == 0) { break; } k2p = mulAndCheck(k2p, k2p); } return result; } catch (MathArithmeticException mae) { mae.getContext().addMessage(LocalizedFormats.OVERFLOW); mae.getContext().addMessage(LocalizedFormats.BASE, k); mae.getContext().addMessage(LocalizedFormats.EXPONENT, e); throw mae; } } 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 testPow() { Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7)); Assert.assertEquals(1, ArithmeticUtils.pow(21, 0)); try { ArithmeticUtils.pow(21, -7); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7)); Assert.assertEquals(1, ArithmeticUtils.pow(21, 0)); try { ArithmeticUtils.pow(21, -7); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } Assert.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7)); Assert.assertEquals(1l, ArithmeticUtils.pow(21l, 0)); try { ArithmeticUtils.pow(21l, -7); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } Assert.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7l)); Assert.assertEquals(1l, ArithmeticUtils.pow(21l, 0l)); try { ArithmeticUtils.pow(21l, -7l); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } BigInteger twentyOne = BigInteger.valueOf(21l); Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7)); Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0)); try { ArithmeticUtils.pow(twentyOne, -7); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7l)); Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0l)); try { ArithmeticUtils.pow(twentyOne, -7l); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(7l))); Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, BigInteger.ZERO)); try { ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(-7l)); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { } BigInteger bigOne = new BigInteger("1543786922199448028351389769265814882661837148" + "4763915343722775611762713982220306372888519211" + "560905579993523402015636025177602059044911261"); Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103)); Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103l)); Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(103l))); }
@Test(expected=MathArithmeticException.class) public void testPowIntOverflow() { ArithmeticUtils.pow(21, 8); }
@Test public void testPowInt() { final int base = 21; Assert.assertEquals(85766121L, ArithmeticUtils.pow(base, 6)); Assert.assertEquals(1801088541L, ArithmeticUtils.pow(base, 7)); }
@Test(expected=MathArithmeticException.class) public void testPowNegativeIntOverflow() { ArithmeticUtils.pow(-21, 8); }
@Test public void testPowNegativeInt() { final int base = -21; Assert.assertEquals(85766121, ArithmeticUtils.pow(base, 6)); Assert.assertEquals(-1801088541, ArithmeticUtils.pow(base, 7)); }
@Test public void testPowMinusOneInt() { final int base = -1; for (int i = 0; i < 100; i++) { final int pow = ArithmeticUtils.pow(base, i); Assert.assertEquals("i: " + i, i % 2 == 0 ? 1 : -1, pow); } }
@Test public void testPowOneInt() { final int base = 1; for (int i = 0; i < 100; i++) { final int pow = ArithmeticUtils.pow(base, i); Assert.assertEquals("i: " + i, 1, pow); } }
@Test(expected=MathArithmeticException.class) public void testPowLongOverflow() { ArithmeticUtils.pow(21, 15); }
@Test public void testPowLong() { final long base = 21; Assert.assertEquals(154472377739119461L, ArithmeticUtils.pow(base, 13)); Assert.assertEquals(3243919932521508681L, ArithmeticUtils.pow(base, 14)); }
@Test(expected=MathArithmeticException.class) public void testPowNegativeLongOverflow() { ArithmeticUtils.pow(-21L, 15); }
@Test public void testPowNegativeLong() { final long base = -21; Assert.assertEquals(-154472377739119461L, ArithmeticUtils.pow(base, 13)); Assert.assertEquals(3243919932521508681L, ArithmeticUtils.pow(base, 14)); }
@Test public void testPowMinusOneLong() { final long base = -1; for (int i = 0; i < 100; i++) { final long pow = ArithmeticUtils.pow(base, i); Assert.assertEquals("i: " + i, i % 2 == 0 ? 1 : -1, pow); } }
@Test public void testPowOneLong() { final long base = 1; for (int i = 0; i < 100; i++) { final long pow = ArithmeticUtils.pow(base, i); Assert.assertEquals("i: " + i, 1, pow); } }