Skip to content

Commit

Permalink
Add iterative version of Euclidean algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Sep 18, 2018
1 parent c00c689 commit 2451db9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import euclideanAlgorithm from '../euclideanAlgorithm';

describe('euclideanAlgorithm', () => {
it('should calculate GCD', () => {
it('should calculate GCD recursively', () => {
expect(euclideanAlgorithm(0, 0)).toBe(0);
expect(euclideanAlgorithm(2, 0)).toBe(2);
expect(euclideanAlgorithm(0, 2)).toBe(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import euclideanAlgorithmIterative from '../euclideanAlgorithmIterative';

describe('euclideanAlgorithmIterative', () => {
it('should calculate GCD iteratively', () => {
expect(euclideanAlgorithmIterative(0, 0)).toBe(0);
expect(euclideanAlgorithmIterative(2, 0)).toBe(2);
expect(euclideanAlgorithmIterative(0, 2)).toBe(2);
expect(euclideanAlgorithmIterative(1, 2)).toBe(1);
expect(euclideanAlgorithmIterative(2, 1)).toBe(1);
expect(euclideanAlgorithmIterative(6, 6)).toBe(6);
expect(euclideanAlgorithmIterative(2, 4)).toBe(2);
expect(euclideanAlgorithmIterative(4, 2)).toBe(2);
expect(euclideanAlgorithmIterative(12, 4)).toBe(4);
expect(euclideanAlgorithmIterative(4, 12)).toBe(4);
expect(euclideanAlgorithmIterative(5, 13)).toBe(1);
expect(euclideanAlgorithmIterative(27, 13)).toBe(1);
expect(euclideanAlgorithmIterative(24, 60)).toBe(12);
expect(euclideanAlgorithmIterative(60, 24)).toBe(12);
expect(euclideanAlgorithmIterative(252, 105)).toBe(21);
expect(euclideanAlgorithmIterative(105, 252)).toBe(21);
expect(euclideanAlgorithmIterative(1071, 462)).toBe(21);
expect(euclideanAlgorithmIterative(462, 1071)).toBe(21);
expect(euclideanAlgorithmIterative(462, -1071)).toBe(21);
expect(euclideanAlgorithmIterative(-462, -1071)).toBe(21);
});
});
18 changes: 4 additions & 14 deletions src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
/**
* Recursive version of Euclidean Algorithm of finding greatest common divisor (GCD).
* @param {number} originalA
* @param {number} originalB
* @return {number}
*/

/*Method 1: A bit Complex to understand*/
export default function euclideanAlgorithm(originalA, originalB) {
// Make input numbers positive.
const a = Math.abs(originalA);
const b = Math.abs(originalB);

// To make algorithm work faster instead of subtracting one number from the other
// we may use modulo operation.
return (b === 0) ? a : euclideanAlgorithm(b, a % b);
}

/*Method 2: Easy to evaluate*/
export default function euclideanAlgorithm2(originalA, originalB) {
const a = Math.abs(originalA);
const b = Math.abs(originalB);

while(a != b){
[a,b] = a>b : [a-b, b] : [a, b-a]
}

return a || b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Iterative version of Euclidean Algorithm of finding greatest common divisor (GCD).
* @param {number} originalA
* @param {number} originalB
* @return {number}
*/
export default function euclideanAlgorithmIterative(originalA, originalB) {
// Make input numbers positive.
let a = Math.abs(originalA);
let b = Math.abs(originalB);

// Subtract one number from another until both numbers would become the same.
// This will be out GCD. Also quit the loop if one of the numbers is zero.
while (a && b && a !== b) {
[a, b] = a > b ? [a - b, b] : [a, b - a];
}

// Return the number that is not equal to zero since the last subtraction (it will be a GCD).
return a || b;
}

0 comments on commit 2451db9

Please sign in to comment.