It's important to return a precise answer, and to support large numbers -
public static BigInteger power(int x, int y) {
return (y > 0) ? power(new BigInteger(String.valueOf(x)), y)
: BigInteger.ONE;
}
public static BigInteger power(BigInteger x, int y) {
return (y > 0) ? power(x, y - 1).multiply(x) : BigInteger.ONE;
}
public static void main(String[] args) {
// From here
// http://en.wikipedia.org/wiki/Power_of_two
String result = "13,407,807,929,942,597,099,574,024,998,205,846,127,479,365,820,592,"
+ "393,377,723,561,443,721,764,030,073,546,976,801,874,298,166,903,427,690,"
+ "031,858,186,486,050,853,753,882,811,946,569,946,433,649,006,084,096";
result = result.replace(",", "");
System.out.println(result.equals(power(2, 512).toString()));
}
Of course, the output is
true
exp(log(x)*y)
? \$\endgroup\$