/// <summary>
/// Equivalent to BigInteger.Pow but with integer arguments.
/// </summary>
/// <param name="radix"> The number to be raised to a power. </param>
/// <param name="exponent"> The number that specifies the power. </param>
/// <returns> The number <paramref name="radix"/> raised to the power
/// <paramref name="exponent"/>. </returns>
public static BigInteger Pow(int radix, int exponent)
{
if (radix < 0 || radix > 36)
{
throw new ArgumentOutOfRangeException("radix");
}
if (exponent < 0)
{
throw new ArgumentOutOfRangeException("exponent");
}
if (radix == 10 && exponent < integerPowersOfTen.Length)
{
// Use a table for quick lookup of powers of 10.
return(new BigInteger(integerPowersOfTen[exponent]));
}
else if (radix == 2)
{
// Power of two is easy.
return(BigInteger.LeftShift(BigInteger.One, exponent));
}
// Special cases.
switch (exponent)
{
case 0:
return(BigInteger.One);
case 1:
return(new BigInteger(radix));
case 2:
return(new BigInteger(radix * radix));
case 3:
return(new BigInteger(radix * radix * radix));
}
// Use recursion to calculate the result.
if ((exponent & 1) == 1)
{
// Exponent is odd.
var temp = Pow(radix, exponent / 2);
return(BigInteger.MultiplyAdd(BigInteger.Multiply(temp, temp), radix, 0));
}
else
{
// Exponent is even.
var temp = Pow(radix, exponent / 2);
return(BigInteger.Multiply(temp, temp));
}
}