-
Notifications
You must be signed in to change notification settings - Fork 19.8k
/
Copy pathBinaryExponentiation.java
42 lines (36 loc) · 1.09 KB
/
BinaryExponentiation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.thealgorithms.divideandconquer;
// Java Program to Implement Binary Exponentiation (power in log n)
// Reference Link: https://en.wikipedia.org/wiki/Exponentiation_by_squaring
/*
* Binary Exponentiation is a method to calculate a to the power of b.
* It is used to calculate a^n in O(log n) time.
*
* Reference:
* https://iq.opengenus.org/binary-exponentiation/
*/
public class BinaryExponentiation {
// recursive function to calculate a to the power of b
public static long calculatePower(long x, long y) {
// Base Case
if (y == 0) {
return 1;
}
if (y % 2 == 1) { // odd power
return x * calculatePower(x, y - 1);
}
return calculatePower(x * x, y / 2); // even power
}
// iterative function to calculate a to the power of b
long power(long n, long m) {
long power = n;
long sum = 1;
while (m > 0) {
if ((m & 1) == 1) {
sum *= power;
}
power = power * power;
m = m >> 1;
}
return sum;
}
}