This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add LCM function * Add index and package * Fix lint issue * Revert package.json changes
- Loading branch information
Showing
4 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,4 +101,4 @@ | |
"kentcdodds/ava" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export default lcm | ||
|
||
/** | ||
* Original source: https://stackoverflow.com/a/38407734 | ||
* | ||
* This method returns least common multiple of two integers | ||
* | ||
* @param {Number} a - first integer | ||
* @param {Number} b - second integer | ||
* @return {Number} - least common multiple | ||
*/ | ||
|
||
function gcd(a, b) { | ||
if (b === 0) { | ||
return a | ||
} else { | ||
return gcd(b, a % b) | ||
} | ||
} | ||
|
||
function lcm(a, b) { | ||
return a * b / gcd(a, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import test from 'ava' | ||
import {lcm} from '../src' | ||
|
||
test('Get lcm of two unequal integers', t => { | ||
const a = 8 | ||
const b = 12 | ||
const expected = 24 | ||
const actual = lcm(a, b) | ||
t.deepEqual(actual, expected) | ||
}) | ||
|
||
test('Get lcm of equal integers', t => { | ||
const a = 11 | ||
const b = 11 | ||
const expected = 11 | ||
const actual = lcm(a, b) | ||
t.deepEqual(actual, expected) | ||
}) |