JavaAlgorithms/DivideAndConquer/BinaryExponentiation.java
2021-10-05 13:59:09 +03:00

17 lines
418 B
Java

public class BinaryExponentiation {
public static void main(String args[]) {
System.out.println(calculatePower(2, 30));
}
// Function to calculate x^y
// Time Complexity: O(logn)
public static long calculatePower(long x, long y) {
if (y == 0) return 1;
long val = calculatePower(x, y / 2);
val *= val;
if (y % 2 == 1) val *= x;
return val;
}
}