-
-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathbinary_exponentiation.ts
37 lines (35 loc) · 1.2 KB
/
binary_exponentiation.ts
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
/**
* @function binaryExponent
* @description Returns the power of a number A raised to the power of B with binary exponentiation
* @summary Binary exponentiation calculatoes A^B in logarithmic time of B instead of linear time
* @param {Number} num - An array of two natural numbers, [A, B], where A^B will be solved
* @return {number} A^B
* @see [Wikipedia](https://cp-algorithms.com/algebra/binary-exp.html)
* @example binaryExponent([5, 2]) = 25
* @example binaryExponent([10, 18]) = 1000000000000000000
*/
export const binaryExponent = (numbers: number[]): number => {
// raise errors upon invalid inputs
if (numbers.length != 2) {
throw new TypeError('Invalid Input')
}
for(let i=0; i < numbers.length; i++){
if (numbers[i] < 0 || !Number.isInteger(numbers[i])) {
throw new TypeError('Invalid Input')
}
}
// binary exponentiation algorithm
// if B == 0
if (numbers[1] === 0) {
// A^0 = 1
return 1;
}
let res = Math.pow(numbers[0], Math.floor(numbers[1] / 2));
// if B is even or odd
if (numbers[1] % 2 === 1) {
return res * res * numbers[0];
}
else {
return res * res;
}
}