Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 9810685

Browse files
ErdtRKent C. Dodds
authored andcommitted
feat(gcd): Add gcd function (#125)
1 parent c3a61db commit 9810685

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/gcd.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default gcd
2+
3+
/**
4+
* This method return greatest common divisor for two numbers
5+
* @param {Number} a - first number
6+
* @param {Number} b - second number
7+
* @return {Number} - greatest common divisor
8+
*/
9+
10+
function gcd(a, b) {
11+
if (!b) {
12+
return a
13+
}
14+
15+
return gcd(b, a % b)
16+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import isNumeric from './is-numeric'
4242
import max from './max'
4343
import slugify from './slugify'
4444
import convertToRoman from './convertToRoman'
45+
import gcd from './gcd'
4546

4647
export {
4748
isOdd,
@@ -88,4 +89,5 @@ export {
8889
max,
8990
slugify,
9091
convertToRoman,
92+
gcd,
9193
}

test/gcd.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava'
2+
import {gcd} from '../src'
3+
4+
test("Get gcd for two integers", t => {
5+
const a = 4;
6+
const b = 64;
7+
const expected = 4;
8+
const actual = gcd(a, b);
9+
t.deepEqual(actual, expected)
10+
})

0 commit comments

Comments
 (0)