Skip to content

fix: cleanup CoPrimeCheck #1609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Maths/CoPrimeCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ const GetEuclidGCD = (arg1, arg2) => {
const CoPrimeCheck = (firstNumber, secondNumber) => {
// firstly, check that input is a number or not.
if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') {
return new TypeError('Argument is not a number.')
throw new TypeError('Argument is not a number.')
}
/*
This is the most efficient algorithm for checking co-primes
if the GCD of both the numbers is 1 that means they are co-primes.
*/
return GetEuclidGCD(firstNumber, secondNumber) === 1
return GetEuclidGCD(Math.abs(firstNumber), Math.abs(secondNumber)) === 1
}

export { CoPrimeCheck }
29 changes: 29 additions & 0 deletions Maths/test/CoPrimeCheck.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CoPrimeCheck } from '../CoPrimeCheck'

describe('CoPrimeCheck', () => {
it.each([
[1, 1],
[1, 2],
[1, 3],
[1, 7],
[20, 21],
[5, 7],
[-5, -7]
])('returns true for %j and %i', (inputA, inputB) => {
expect(CoPrimeCheck(inputA, inputB)).toBe(true)
expect(CoPrimeCheck(inputB, inputA)).toBe(true)
})

it.each([
[5, 15],
[13 * 17 * 19, 17 * 23 * 29]
])('returns false for %j and %i', (inputA, inputB) => {
expect(CoPrimeCheck(inputA, inputB)).toBe(false)
expect(CoPrimeCheck(inputB, inputA)).toBe(false)
})

it('should throw when any of the inputs is not a number', () => {
expect(() => CoPrimeCheck('1', 2)).toThrowError()
expect(() => CoPrimeCheck(1, '2')).toThrowError()
})
})