We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 7861ca6 + 7a1c4b0 commit 295451fCopy full SHA for 295451f
1 file changed
DivideAndConquer/BinaryExponentiation.java
@@ -0,0 +1,16 @@
1
+public class BinaryExponentiation {
2
+
3
+ public static void main(String args[]) {
4
+ System.out.println(calculatePower(2, 30));
5
+ }
6
7
+ // Function to calculate x^y
8
+ // Time Complexity: O(logn)
9
+ public static long calculatePower(long x, long y) {
10
+ if (y == 0) return 1;
11
+ long val = calculatePower(x, y / 2);
12
+ val *= val;
13
+ if (y % 2 == 1) val *= x;
14
+ return val;
15
16
+}
0 commit comments