Skip to content

Commit 2451db9

Browse files
committedSep 18, 2018
Add iterative version of Euclidean algorithm.
1 parent c00c689 commit 2451db9

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed
 

‎src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js ‎src/algorithms/math/euclidean-algorithm/__test__/euclideanAlgorithm.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import euclideanAlgorithm from '../euclideanAlgorithm';
22

33
describe('euclideanAlgorithm', () => {
4-
it('should calculate GCD', () => {
4+
it('should calculate GCD recursively', () => {
55
expect(euclideanAlgorithm(0, 0)).toBe(0);
66
expect(euclideanAlgorithm(2, 0)).toBe(2);
77
expect(euclideanAlgorithm(0, 2)).toBe(2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import euclideanAlgorithmIterative from '../euclideanAlgorithmIterative';
2+
3+
describe('euclideanAlgorithmIterative', () => {
4+
it('should calculate GCD iteratively', () => {
5+
expect(euclideanAlgorithmIterative(0, 0)).toBe(0);
6+
expect(euclideanAlgorithmIterative(2, 0)).toBe(2);
7+
expect(euclideanAlgorithmIterative(0, 2)).toBe(2);
8+
expect(euclideanAlgorithmIterative(1, 2)).toBe(1);
9+
expect(euclideanAlgorithmIterative(2, 1)).toBe(1);
10+
expect(euclideanAlgorithmIterative(6, 6)).toBe(6);
11+
expect(euclideanAlgorithmIterative(2, 4)).toBe(2);
12+
expect(euclideanAlgorithmIterative(4, 2)).toBe(2);
13+
expect(euclideanAlgorithmIterative(12, 4)).toBe(4);
14+
expect(euclideanAlgorithmIterative(4, 12)).toBe(4);
15+
expect(euclideanAlgorithmIterative(5, 13)).toBe(1);
16+
expect(euclideanAlgorithmIterative(27, 13)).toBe(1);
17+
expect(euclideanAlgorithmIterative(24, 60)).toBe(12);
18+
expect(euclideanAlgorithmIterative(60, 24)).toBe(12);
19+
expect(euclideanAlgorithmIterative(252, 105)).toBe(21);
20+
expect(euclideanAlgorithmIterative(105, 252)).toBe(21);
21+
expect(euclideanAlgorithmIterative(1071, 462)).toBe(21);
22+
expect(euclideanAlgorithmIterative(462, 1071)).toBe(21);
23+
expect(euclideanAlgorithmIterative(462, -1071)).toBe(21);
24+
expect(euclideanAlgorithmIterative(-462, -1071)).toBe(21);
25+
});
26+
});
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
/**
2+
* Recursive version of Euclidean Algorithm of finding greatest common divisor (GCD).
23
* @param {number} originalA
34
* @param {number} originalB
45
* @return {number}
56
*/
6-
7-
/*Method 1: A bit Complex to understand*/
87
export default function euclideanAlgorithm(originalA, originalB) {
8+
// Make input numbers positive.
99
const a = Math.abs(originalA);
1010
const b = Math.abs(originalB);
1111

12+
// To make algorithm work faster instead of subtracting one number from the other
13+
// we may use modulo operation.
1214
return (b === 0) ? a : euclideanAlgorithm(b, a % b);
1315
}
14-
15-
/*Method 2: Easy to evaluate*/
16-
export default function euclideanAlgorithm2(originalA, originalB) {
17-
const a = Math.abs(originalA);
18-
const b = Math.abs(originalB);
19-
20-
while(a != b){
21-
[a,b] = a>b : [a-b, b] : [a, b-a]
22-
}
23-
24-
return a || b;
25-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Iterative version of Euclidean Algorithm of finding greatest common divisor (GCD).
3+
* @param {number} originalA
4+
* @param {number} originalB
5+
* @return {number}
6+
*/
7+
export default function euclideanAlgorithmIterative(originalA, originalB) {
8+
// Make input numbers positive.
9+
let a = Math.abs(originalA);
10+
let b = Math.abs(originalB);
11+
12+
// Subtract one number from another until both numbers would become the same.
13+
// This will be out GCD. Also quit the loop if one of the numbers is zero.
14+
while (a && b && a !== b) {
15+
[a, b] = a > b ? [a - b, b] : [a, b - a];
16+
}
17+
18+
// Return the number that is not equal to zero since the last subtraction (it will be a GCD).
19+
return a || b;
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.