Skip to content

Commit f42cdf8

Browse files
committed
Added gcd function
1 parent 996a053 commit f42cdf8

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// tslint:disable:no-expression-statement
2+
import test from 'ava';
3+
import { greatestCommonDivisor } from './greatest-common-divisor';
4+
5+
test('finds GCD of positive numbers', t => {
6+
const tests = [
7+
[[2, 5], 1],
8+
[[1000026, 2330], 2],
9+
[[100000004, 244127], 13],
10+
[[1000000001, 513], 19]
11+
];
12+
for (const vtest of tests) {
13+
const [v, a] = vtest;
14+
// @ts-ignore
15+
t.is(greatestCommonDivisor(...v), a);
16+
}
17+
});
18+
19+
test('finds GCD of negative numbers', t => {
20+
const tests = [
21+
[[-2, -5], 1],
22+
[[-1000026, -2330], 2],
23+
[[-100000004, -244127], 13],
24+
[[-1000000001, -513], 19]
25+
];
26+
for (const vtest of tests) {
27+
const [v, a] = vtest;
28+
// @ts-ignore
29+
t.is(greatestCommonDivisor(...v), a);
30+
}
31+
});
32+
33+
test('finds GCD of 1 and 1', t => {
34+
t.is(greatestCommonDivisor(1, 1), 1);
35+
});
36+
37+
test('works GCD with zero values', t => {
38+
t.is(greatestCommonDivisor(0, 0), 1);
39+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Returns greatest common divider from number using iterative Euclid's algorithm
3+
* @link https://en.wikipedia.org/wiki/Euclidean_algorithm
4+
*
5+
* Iterative algorithm much faster (in average ~1.335) than recursive
6+
*
7+
* @param {number} a
8+
* @param {number} b
9+
* @returns {number}
10+
*/
11+
export function greatestCommonDivisor(a: number, b: number): number {
12+
a = Math.abs(a);
13+
b = Math.abs(b);
14+
15+
if (a === b && a === 0) {
16+
return 1;
17+
}
18+
19+
if (b > a) {
20+
const temp = a;
21+
a = b;
22+
b = temp;
23+
}
24+
25+
while (true) {
26+
a %= b;
27+
if (a === 0) {
28+
return b;
29+
}
30+
b %= a;
31+
if (b === 0) {
32+
return a;
33+
}
34+
}
35+
}

src/lib/math/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './is-power-of-two/is-power-of-two';
22
export * from './nearest-highest-power-of-two/nearest-highest-power-of-two';
33
export * from './nearest-highest-power-of-two-result/nearest-highest-power-of-two-result';
4+
export * from './greatest-common-divisor/greatest-common-divisor';

yarn.lock

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ inflight@^1.0.4:
20532053
once "^1.3.0"
20542054
wrappy "1"
20552055

2056-
inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.3:
2056+
inherits@2, inherits@^2.0.3, inherits@~2.0.3:
20572057
version "2.0.4"
20582058
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
20592059
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
@@ -3653,14 +3653,6 @@ set-blocking@^2.0.0:
36533653
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
36543654
integrity sha1-BF+XgtARrppoA93TgrJDkrPYkPc=
36553655

3656-
sha.js@^2.4.11:
3657-
version "2.4.11"
3658-
resolved "https://registry.yarnpkg.com/sha.js/-/sha.js-2.4.11.tgz#37a5cf0b81ecbc6943de109ba2960d1b26584ae7"
3659-
integrity sha512-QMEp5B7cftE7APOjk5Y6xgrbWu+WkLVQwk8JNjZ8nKRciZaByEW6MubieAiToS7+dwvrjGhH8jRXz3MVd0AYqQ==
3660-
dependencies:
3661-
inherits "^2.0.1"
3662-
safe-buffer "^5.0.1"
3663-
36643656
shebang-command@^1.2.0:
36653657
version "1.2.0"
36663658
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"

0 commit comments

Comments
 (0)