Refactor fast powering algorithm.

This commit is contained in:
Oleksii Trekhleb 2018-09-04 17:35:48 +03:00
parent 8676c1b9fe
commit 8116aa7cfb
5 changed files with 38 additions and 25 deletions

View File

@ -67,6 +67,7 @@ a set of rules that precisely define a sequence of operations.
* `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle) * `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle)
* `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them * `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them
* `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion * `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion
* `B` [Fast Powering](src/algorithms/math/fast-powering)
* `A` [Integer Partition](src/algorithms/math/integer-partition) * `A` [Integer Partition](src/algorithms/math/integer-partition)
* `A` [Liu Hui π Algorithm](src/algorithms/math/liu-hui) - approximate π calculations based on N-gons * `A` [Liu Hui π Algorithm](src/algorithms/math/liu-hui) - approximate π calculations based on N-gons
* `A` [Discrete Fourier Transform](src/algorithms/math/fourier-transform) - decompose a function of time (a signal) into the frequencies that make it up * `A` [Discrete Fourier Transform](src/algorithms/math/fourier-transform) - decompose a function of time (a signal) into the frequencies that make it up
@ -163,6 +164,7 @@ algorithm is an abstraction higher than a computer program.
* `B` [Tree Depth-First Search](src/algorithms/tree/depth-first-search) (DFS) * `B` [Tree Depth-First Search](src/algorithms/tree/depth-first-search) (DFS)
* `B` [Graph Depth-First Search](src/algorithms/graph/depth-first-search) (DFS) * `B` [Graph Depth-First Search](src/algorithms/graph/depth-first-search) (DFS)
* `B` [Jump Game](src/algorithms/uncategorized/jump-game) * `B` [Jump Game](src/algorithms/uncategorized/jump-game)
* `B` [Fast Powering](src/algorithms/math/fast-powering)
* `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions) * `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions)
* `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions) * `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions)
* **Dynamic Programming** - build up a solution using previously found sub-solutions * **Dynamic Programming** - build up a solution using previously found sub-solutions

View File

@ -1,16 +0,0 @@
import computePower from '../power';
describe('computePower', () => {
it('should compute Power', () => {
expect(computePower(1, 1)).toBe(1);
expect(computePower(2, 0)).toBe(1);
expect(computePower(3, 4)).toBe(81);
expect(computePower(190, 2)).toBe(36100);
expect(computePower(16, 16)).toBe(18446744073709552000);
expect(computePower(100, 9)).toBe(1000000000000000000);
expect(computePower(9, 16)).toBe(1853020188851841);
expect(computePower(11, 5)).toBe(161051);
expect(computePower(13, 11)).toBe(1792160394037);
expect(computePower(7, 21)).toBe(558545864083284000);
});
});

View File

@ -1,4 +1,4 @@
# Power(a,b) # Fast Powering Algorithm
This computes power of (a,b) This computes power of (a,b)
eg: power(2,3) = 8 eg: power(2,3) = 8
@ -33,3 +33,7 @@ power(2,5) = 2 * power(2,2) * power(2,2) = 2 * 4 * 4 = 32
Complexity relation: T(n) = T(n/2) + 1 Complexity relation: T(n) = T(n/2) + 1
Time complexity of the algorithm: O(logn) Time complexity of the algorithm: O(logn)
## References

View File

@ -0,0 +1,23 @@
import fastPowering from '../fastPowering';
describe('fastPowering', () => {
it('should compute power in log(n) time', () => {
expect(fastPowering(1, 1)).toBe(1);
expect(fastPowering(2, 0)).toBe(1);
expect(fastPowering(2, 2)).toBe(4);
expect(fastPowering(2, 3)).toBe(8);
expect(fastPowering(2, 4)).toBe(16);
expect(fastPowering(2, 5)).toBe(32);
expect(fastPowering(2, 6)).toBe(64);
expect(fastPowering(2, 7)).toBe(128);
expect(fastPowering(2, 8)).toBe(256);
expect(fastPowering(3, 4)).toBe(81);
expect(fastPowering(190, 2)).toBe(36100);
expect(fastPowering(11, 5)).toBe(161051);
expect(fastPowering(13, 11)).toBe(1792160394037);
expect(fastPowering(9, 16)).toBe(1853020188851841);
expect(fastPowering(16, 16)).toBe(18446744073709552000);
expect(fastPowering(7, 21)).toBe(558545864083284000);
expect(fastPowering(100, 9)).toBe(1000000000000000000);
});
});

View File

@ -1,11 +1,11 @@
/** /**
* @param {number1} number * Recursive implementation to compute power.
* @param {number2} number *
* @return {number1^number2} * @param {number} number1
* @param {number} number2
* @return {number}
*/ */
export default function fastPowering(number1, number2) {
// recursive implementation to compute power
export default function computePower(number1, number2) {
let val = 0; let val = 0;
let res = 0; let res = 0;
if (number2 === 0) { // if number2 is 0 if (number2 === 0) { // if number2 is 0
@ -13,10 +13,10 @@ export default function computePower(number1, number2) {
} else if (number2 === 1) { // if number2 is 1 return number 1 as it is } else if (number2 === 1) { // if number2 is 1 return number 1 as it is
val = number1; val = number1;
} else if (number2 % 2 === 0) { // if number2 is even } else if (number2 % 2 === 0) { // if number2 is even
res = computePower(number1, number2 / 2); res = fastPowering(number1, number2 / 2);
val = res * res; val = res * res;
} else { // if number2 is odd } else { // if number2 is odd
res = computePower(number1, Math.floor(number2 / 2)); res = fastPowering(number1, Math.floor(number2 / 2));
val = res * res * number1; val = res * res * number1;
} }
return val; return val;