Skip to content

Commit 5341547

Browse files
committedSep 3, 2018
Added fast-exponentiation algorithm
1 parent 6b0bacd commit 5341547

File tree

7 files changed

+115
-10
lines changed

7 files changed

+115
-10
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ a set of rules that precisely define a sequence of operations.
6060
* `B` [Factorial](src/algorithms/math/factorial)
6161
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
6262
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
63-
* `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
63+
* `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest
64+
Common Divisor (GCD)
65+
* `B` [Fast Exponentiation](src/algorithms/math/fast-exponentiation)
6466
* `B` [Least Common Multiple](src/algorithms/math/least-common-multiple) (LCM)
6567
* `B` [Sieve of Eratosthenes](src/algorithms/math/sieve-of-eratosthenes) - finding all prime numbers up to any given limit
6668
* `B` [Is Power of Two](src/algorithms/math/is-power-of-two) - check if the number is power of two (naive and bitwise algorithms)

‎package-lock.json

+27-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Fast Exponentiation Algorithm
2+
The operation of modular exponentiation calculates the remainder when an integer `b` (the base) raised to the eth power (the exponent), be, is divided by a positive integer m (the modulus). In symbols, given base `b`, exponent e, and modulus m, the modular exponentiation `c` is: `c ≡ b^e (mod m)`.
3+
4+
# Modular Arithmetic
5+
- `(a + b) % m` = `( a%m + b%m ) % m`
6+
- `(a * b) % m` = `( a%m * b%m ) % m`
7+
8+
9+
## References
10+
- [Wikipedia](https://en.wikipedia.org/wiki/Modular_exponentiation)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import fastExponent from '../fastExponent';
2+
3+
describe('fastExponent', () => {
4+
it('should calculate power using fast exponentiation algorithm', () => {
5+
expect(fastExponent(5, 5)).toBe(3125);
6+
});
7+
it('should calculate power using fast exponentiation algorithm', () => {
8+
expect(fastExponent(123, 4)).toBe(228886641);
9+
});
10+
it('should calculate power using fast exponentiation algorithm', () => {
11+
expect(fastExponent(2, 15)).toBe(32768);
12+
});
13+
it('should calculate power using fast exponentiation algorithm', () => {
14+
expect(fastExponent(43, 3)).toBe(79507);
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import modularExponent from '../modularExponent';
2+
3+
describe('modularExponent', () => {
4+
it('should calculate power using fast exponentiation algorithm', () => {
5+
expect(modularExponent(5, 5, 23)).toBe(20);
6+
});
7+
it('should calculate power using fast exponentiation algorithm', () => {
8+
expect(modularExponent(123, 4, 10001)).toBe(3755);
9+
});
10+
it('should calculate power using fast exponentiation algorithm', () => {
11+
expect(modularExponent(2, 15, 13)).toBe(8);
12+
});
13+
it('should calculate power using fast exponentiation algorithm', () => {
14+
expect(modularExponent(43, 3, 1000000007)).toBe(79507);
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} base
3+
* @param {number} exponent
4+
* @return {number}
5+
*/
6+
7+
export default function fastExponent(base, exponent) {
8+
let x = base;
9+
let y = exponent;
10+
let res = 1;
11+
12+
13+
while (y > 0) {
14+
if (y & 1) res *= x;
15+
y >>= 1;
16+
x *= x;
17+
}
18+
19+
return res;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} base
3+
* @param {number} exponent
4+
* @param {number} m
5+
* @return {number}
6+
*/
7+
8+
export default function modularExponent(base, exponent, m) {
9+
let x = base;
10+
let y = exponent;
11+
let res = 1;
12+
const p = m;
13+
14+
x %= p;
15+
16+
while (y > 0) {
17+
if (y & 1) res = (res * x) % p;
18+
y >>= 1;
19+
x = (x * x) % p;
20+
}
21+
22+
return res;
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.