File tree Expand file tree Collapse file tree 2 files changed +8
-15
lines changed Expand file tree Collapse file tree 2 files changed +8
-15
lines changed Original file line number Diff line number Diff line change 9
9
is coprime with b.
10
10
*/
11
11
12
- // Here we use a GetEuclidGCD method as a utility.
13
- const GetEuclidGCD = (arg1, arg2) => {
14
- let less = arg1 > arg2 ? arg2 : arg1
15
- for (less; less >= 2; less--) {
16
- if (arg1 % less === 0 && arg2 % less === 0) return less
17
- }
18
- return less
19
- }
12
+ import { GetEuclidGCD } from './GetEuclidGCD'
20
13
21
14
// CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
22
15
/**
@@ -26,15 +19,11 @@ const GetEuclidGCD = (arg1, arg2) => {
26
19
* @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`.
27
20
*/
28
21
const CoPrimeCheck = (firstNumber, secondNumber) => {
29
- // firstly, check that input is a number or not.
30
- if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') {
31
- throw new TypeError('Argument is not a number.')
32
- }
33
22
/*
34
23
This is the most efficient algorithm for checking co-primes
35
24
if the GCD of both the numbers is 1 that means they are co-primes.
36
25
*/
37
- return GetEuclidGCD(Math.abs( firstNumber), Math.abs( secondNumber) ) === 1
26
+ return GetEuclidGCD(firstNumber, secondNumber) === 1
38
27
}
39
28
40
29
export { CoPrimeCheck }
Original file line number Diff line number Diff line change @@ -8,15 +8,19 @@ describe('CoPrimeCheck', () => {
8
8
[1, 7],
9
9
[20, 21],
10
10
[5, 7],
11
- [-5, -7]
11
+ [-5, -7],
12
+ [1, 0],
13
+ [-1, 0]
12
14
])('returns true for %j and %i', (inputA, inputB) => {
13
15
expect(CoPrimeCheck(inputA, inputB)).toBe(true)
14
16
expect(CoPrimeCheck(inputB, inputA)).toBe(true)
15
17
})
16
18
17
19
it.each([
18
20
[5, 15],
19
- [13 * 17 * 19, 17 * 23 * 29]
21
+ [13 * 17 * 19, 17 * 23 * 29],
22
+ [2, 0],
23
+ [0, 0]
20
24
])('returns false for %j and %i', (inputA, inputB) => {
21
25
expect(CoPrimeCheck(inputA, inputB)).toBe(false)
22
26
expect(CoPrimeCheck(inputB, inputA)).toBe(false)
You can’t perform that action at this time.
0 commit comments