Skip to content

Commit 8676c1b

Browse files
HatimLokhandwalatrekhleb
authored andcommittedSep 4, 2018
Adding math algorithm to compute power and its tests (trekhleb#172)
* Adding math algorithm to compute power and its tests * adding more test cases, updating compute power js * Updating ReadMe for power computation algorithm
1 parent 518dc57 commit 8676c1b

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Power(a,b)
2+
3+
This computes power of (a,b)
4+
eg: power(2,3) = 8
5+
power(10,0) = 1
6+
7+
The algorithm uses divide and conquer approach to compute power.
8+
Currently the algorithm work for two positive integers X and Y
9+
Lets say there are two numbers X and Y.
10+
At each step of the algorithm:
11+
1. if Y is even
12+
then power(X, Y/2) * power(X, Y/2) is computed
13+
2. if Y is odd
14+
then X * power(X, Y/2) * power(X, Y/2) is computed
15+
16+
At each step since power(X,Y/2) is called twice, this is optimised by saving the result of power(X, Y/2) in a variable (lets say res).
17+
And then res is multiplied by self.
18+
19+
Illustration through example
20+
power (2,5)
21+
- 2 * power(2,2) * power(2,2)
22+
power(2,2)
23+
- power(2,1) * power(2,1)
24+
power(2,1)
25+
- return 2
26+
27+
Going up the tree once the end values are computed
28+
power(2,1) = 2
29+
power(2,2) = power(2,1) * power(2,1) = 2 * 2 = 4
30+
power(2,5) = 2 * power(2,2) * power(2,2) = 2 * 4 * 4 = 32
31+
32+
33+
Complexity relation: T(n) = T(n/2) + 1
34+
35+
Time complexity of the algorithm: O(logn)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import computePower from '../power';
2+
3+
describe('computePower', () => {
4+
it('should compute Power', () => {
5+
expect(computePower(1, 1)).toBe(1);
6+
expect(computePower(2, 0)).toBe(1);
7+
expect(computePower(3, 4)).toBe(81);
8+
expect(computePower(190, 2)).toBe(36100);
9+
expect(computePower(16, 16)).toBe(18446744073709552000);
10+
expect(computePower(100, 9)).toBe(1000000000000000000);
11+
expect(computePower(9, 16)).toBe(1853020188851841);
12+
expect(computePower(11, 5)).toBe(161051);
13+
expect(computePower(13, 11)).toBe(1792160394037);
14+
expect(computePower(7, 21)).toBe(558545864083284000);
15+
});
16+
});
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number1} number
3+
* @param {number2} number
4+
* @return {number1^number2}
5+
*/
6+
7+
// recursive implementation to compute power
8+
export default function computePower(number1, number2) {
9+
let val = 0;
10+
let res = 0;
11+
if (number2 === 0) { // if number2 is 0
12+
val = 1;
13+
} else if (number2 === 1) { // if number2 is 1 return number 1 as it is
14+
val = number1;
15+
} else if (number2 % 2 === 0) { // if number2 is even
16+
res = computePower(number1, number2 / 2);
17+
val = res * res;
18+
} else { // if number2 is odd
19+
res = computePower(number1, Math.floor(number2 / 2));
20+
val = res * res * number1;
21+
}
22+
return val;
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.