Skip to content

Commit 0e0cf98

Browse files
authored
fix: cleanup CoPrimeCheck (TheAlgorithms#1609)
1 parent fb0a99c commit 0e0cf98

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Maths/CoPrimeCheck.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ const GetEuclidGCD = (arg1, arg2) => {
2828
const CoPrimeCheck = (firstNumber, secondNumber) => {
2929
// firstly, check that input is a number or not.
3030
if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') {
31-
return new TypeError('Argument is not a number.')
31+
throw new TypeError('Argument is not a number.')
3232
}
3333
/*
3434
This is the most efficient algorithm for checking co-primes
3535
if the GCD of both the numbers is 1 that means they are co-primes.
3636
*/
37-
return GetEuclidGCD(firstNumber, secondNumber) === 1
37+
return GetEuclidGCD(Math.abs(firstNumber), Math.abs(secondNumber)) === 1
3838
}
3939

4040
export { CoPrimeCheck }

Maths/test/CoPrimeCheck.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { CoPrimeCheck } from '../CoPrimeCheck'
2+
3+
describe('CoPrimeCheck', () => {
4+
it.each([
5+
[1, 1],
6+
[1, 2],
7+
[1, 3],
8+
[1, 7],
9+
[20, 21],
10+
[5, 7],
11+
[-5, -7]
12+
])('returns true for %j and %i', (inputA, inputB) => {
13+
expect(CoPrimeCheck(inputA, inputB)).toBe(true)
14+
expect(CoPrimeCheck(inputB, inputA)).toBe(true)
15+
})
16+
17+
it.each([
18+
[5, 15],
19+
[13 * 17 * 19, 17 * 23 * 29]
20+
])('returns false for %j and %i', (inputA, inputB) => {
21+
expect(CoPrimeCheck(inputA, inputB)).toBe(false)
22+
expect(CoPrimeCheck(inputB, inputA)).toBe(false)
23+
})
24+
25+
it('should throw when any of the inputs is not a number', () => {
26+
expect(() => CoPrimeCheck('1', 2)).toThrowError()
27+
expect(() => CoPrimeCheck(1, '2')).toThrowError()
28+
})
29+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy