Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: svr8/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 2 commits
  • 7 files changed
  • 1 contributor

Commits on Sep 3, 2018

  1. Copy the full SHA
    5341547 View commit details

Commits on Sep 4, 2018

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8e6de4c View commit details
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;
}