Looking for optimization, smart tips and verification of complexity: O (log (base 2) power).
NOTE: System.out.println("Expected 16, Actual: " + Power.pow(2, 7)); is a typo. It correctly returns 128.
/** * Find power of a number. * * Complexity: O (log (base 2) power) */ public final class Power { private Power() { } /** * Finds the power of the input number. * @param x the number whose power needs to be found * @param pow the power * @return the value of the number raised to the power. */ public static double pow(double x, int pow) { if (x == 0) return 1; return pow > 0 ? getPositivePower(x, pow) : 1 / getPositivePower(x, -pow); } private static double getPositivePower(double x, int pow) { assert x != 0; if (pow == 0) return 1; int currentPow = 1; double value = x; while (currentPow <= pow/2) { value = value * value; currentPow = currentPow * 2; } return value * getPositivePower(x, pow - currentPow); } public static void main(String[] args) { System.out.println("Expected 6.25, Actual: " + Power.pow(2.5, 2)); System.out.println("Expected 16, Actual: " + Power.pow(2, 7)); System.out.println("Expected 0.25, Actual: " + Power.pow(2, -2)); System.out.println("Expected -27, Actual: " + Power.pow(-3, 3)); } }
java.lang.Math.powfunction. \$\endgroup\$