Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fast-exponentiation algorithm #199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -60,7 +60,9 @@ a set of rules that precisely define a sequence of operations.
* `B` [Factorial](src/algorithms/math/factorial)
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
* `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
* `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest
Common Divisor (GCD)
* `B` [Fast Exponentiation](src/algorithms/math/fast-exponentiation)
* `B` [Least Common Multiple](src/algorithms/math/least-common-multiple) (LCM)
* `B` [Sieve of Eratosthenes](src/algorithms/math/sieve-of-eratosthenes) - finding all prime numbers up to any given limit
* `B` [Is Power of Two](src/algorithms/math/is-power-of-two) - check if the number is power of two (naive and bitwise algorithms)
36 changes: 27 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/algorithms/math/fast-exponentiation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Fast Exponentiation Algorithm
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)`.

# Modular Arithmetic
- `(a + b) % m` = `( a%m + b%m ) % m`
- `(a * b) % m` = `( a%m * b%m ) % m`


## References
- [Wikipedia](https://en.wikipedia.org/wiki/Modular_exponentiation)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fastExponent from '../fastExponent';

describe('fastExponent', () => {
it('should calculate power using fast exponentiation algorithm', () => {
expect(fastExponent(5, 5)).toBe(3125);
});
it('should calculate power using fast exponentiation algorithm', () => {
expect(fastExponent(123, 4)).toBe(228886641);
});
it('should calculate power using fast exponentiation algorithm', () => {
expect(fastExponent(2, 15)).toBe(32768);
});
it('should calculate power using fast exponentiation algorithm', () => {
expect(fastExponent(43, 3)).toBe(79507);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import modularExponent from '../modularExponent';

describe('modularExponent', () => {
it('should calculate power using fast exponentiation algorithm', () => {
expect(modularExponent(5, 5, 23)).toBe(20);
});
it('should calculate power using fast exponentiation algorithm', () => {
expect(modularExponent(123, 4, 10001)).toBe(3755);
});
it('should calculate power using fast exponentiation algorithm', () => {
expect(modularExponent(2, 15, 13)).toBe(8);
});
it('should calculate power using fast exponentiation algorithm', () => {
expect(modularExponent(43, 3, 1000000007)).toBe(79507);
});
});
20 changes: 20 additions & 0 deletions src/algorithms/math/fast-exponentiation/fastExponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number} base
* @param {number} exponent
* @return {number}
*/

export default function fastExponent(base, exponent) {
let x = base;
let y = exponent;
let res = 1;


while (y > 0) {
if (y & 1) res *= x;
y >>= 1;
x *= x;
}

return res;
}
23 changes: 23 additions & 0 deletions src/algorithms/math/fast-exponentiation/modularExponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number} base
* @param {number} exponent
* @param {number} m
* @return {number}
*/

export default function modularExponent(base, exponent, m) {
let x = base;
let y = exponent;
let res = 1;
const p = m;

x %= p;

while (y > 0) {
if (y & 1) res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}

return res;
}