Skip to content

Commit 4fc5483

Browse files
committedApr 17, 2018
Add primality tests.
1 parent 54f6aad commit 4fc5483

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
* [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci)
3232
* [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/cartesian-product)
3333
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set)
34-
* [Primality Test](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test)
34+
* [Primality Test](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-test) (Trial Division)
35+
* [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the greatest common divisor (GCD)
3536
* Collatz Conjecture algorithm
3637
* Extended Euclidean algorithm
37-
* Euclidean algorithm to calculate the Greatest Common Divisor (GCD)
3838
* Find Divisors
3939
* Fisher-Yates
4040
* Greatest Difference
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Euclidean algorithm
2+
3+
In mathematics, the Euclidean algorithm, or Euclid's algorithm,
4+
is an efficient method for computing the greatest common divisor
5+
(GCD) of two numbers, the largest number that divides both of
6+
them without leaving a remainder.
7+
8+
The Euclidean algorithm is based on the principle that the
9+
greatest common divisor of two numbers does not change if
10+
the larger number is replaced by its difference with the
11+
smaller number. For example, `21` is the GCD of `252` and
12+
`105` (as `252 = 21 × 12` and `105 = 21 × 5`), and the same
13+
number `21` is also the GCD of `105` and `252 − 105 = 147`.
14+
Since this replacement reduces the larger of the two numbers,
15+
repeating this process gives successively smaller pairs of
16+
numbers until the two numbers become equal.
17+
When that occurs, they are the GCD of the original two numbers.
18+
19+
By reversing the steps, the GCD can be expressed as a sum of
20+
the two original numbers each multiplied by a positive or
21+
negative integer, e.g., `21 = 5 × 105 + (−2) × 252`.
22+
The fact that the GCD can always be expressed in this way is
23+
known as Bézout's identity.
24+
25+
![GCD](https://upload.wikimedia.org/wikipedia/commons/3/37/Euclid%27s_algorithm_Book_VII_Proposition_2_3.png)
26+
27+
Euclid's method for finding the greatest common divisor (GCD)
28+
of two starting lengths `BA` and `DC`, both defined to be
29+
multiples of a common "unit" length. The length `DC` being
30+
shorter, it is used to "measure" `BA`, but only once because
31+
remainder `EA` is less than `DC`. EA now measures (twice)
32+
the shorter length `DC`, with remainder `FC` shorter than `EA`.
33+
Then `FC` measures (three times) length `EA`. Because there is
34+
no remainder, the process ends with `FC` being the `GCD`.
35+
On the right Nicomachus' example with numbers `49` and `21`
36+
resulting in their GCD of `7` (derived from Heath 1908:300).
37+
38+
![GCD](https://upload.wikimedia.org/wikipedia/commons/7/74/24x60.svg)
39+
40+
A `24-by-60` rectangle is covered with ten `12-by-12` square
41+
tiles, where `12` is the GCD of `24` and `60`. More generally,
42+
an `a-by-b` rectangle can be covered with square tiles of
43+
side-length `c` only if `c` is a common divisor of `a` and `b`.
44+
45+
![GCD](https://upload.wikimedia.org/wikipedia/commons/1/1c/Euclidean_algorithm_1071_462.gif)
46+
47+
Subtraction-based animation of the Euclidean algorithm.
48+
The initial rectangle has dimensions `a = 1071` and `b = 462`.
49+
Squares of size `462×462` are placed within it leaving a
50+
`462×147` rectangle. This rectangle is tiled with `147×147`
51+
squares until a `21×147` rectangle is left, which in turn is
52+
tiled with `21×21` squares, leaving no uncovered area.
53+
The smallest square size, `21`, is the GCD of `1071` and `462`.
54+
55+
## References
56+
57+
[Wikipedia](https://en.wikipedia.org/wiki/Euclidean_algorithm)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import euclideanAlgorithm from '../euclideanAlgorithm';
2+
3+
describe('euclideanAlgorithm', () => {
4+
it('should calculate GCD', () => {
5+
expect(euclideanAlgorithm(0, 0)).toBeNull();
6+
expect(euclideanAlgorithm(2, 0)).toBe(2);
7+
expect(euclideanAlgorithm(0, 2)).toBe(2);
8+
expect(euclideanAlgorithm(1, 2)).toBe(1);
9+
expect(euclideanAlgorithm(2, 1)).toBe(1);
10+
expect(euclideanAlgorithm(6, 6)).toBe(6);
11+
expect(euclideanAlgorithm(2, 4)).toBe(2);
12+
expect(euclideanAlgorithm(4, 2)).toBe(2);
13+
expect(euclideanAlgorithm(12, 4)).toBe(4);
14+
expect(euclideanAlgorithm(4, 12)).toBe(4);
15+
expect(euclideanAlgorithm(5, 13)).toBe(1);
16+
expect(euclideanAlgorithm(27, 13)).toBe(1);
17+
expect(euclideanAlgorithm(24, 60)).toBe(12);
18+
expect(euclideanAlgorithm(60, 24)).toBe(12);
19+
expect(euclideanAlgorithm(252, 105)).toBe(21);
20+
expect(euclideanAlgorithm(105, 252)).toBe(21);
21+
expect(euclideanAlgorithm(1071, 462)).toBe(21);
22+
expect(euclideanAlgorithm(462, 1071)).toBe(21);
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number} a
3+
* @param {number} b
4+
* @return {number|null}
5+
*/
6+
export default function euclideanAlgorithm(a, b) {
7+
if (a === 0 && b === 0) {
8+
return null;
9+
}
10+
11+
if (a === 0 && b !== 0) {
12+
return b;
13+
}
14+
15+
if (a !== 0 && b === 0) {
16+
return a;
17+
}
18+
19+
if (a > b) {
20+
return euclideanAlgorithm(a - b, b);
21+
}
22+
23+
return euclideanAlgorithm(b - a, a);
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.