fix: handle zeros in CoPrimeCheck (#1622)

This commit is contained in:
Piotr Idzik
2024-02-29 06:06:08 +01:00
committed by GitHub
parent a5945e37c2
commit 8734dfccf9
2 changed files with 8 additions and 15 deletions

View File

@ -9,14 +9,7 @@
is coprime with b.
*/
// Here we use a GetEuclidGCD method as a utility.
const GetEuclidGCD = (arg1, arg2) => {
let less = arg1 > arg2 ? arg2 : arg1
for (less; less >= 2; less--) {
if (arg1 % less === 0 && arg2 % less === 0) return less
}
return less
}
import { GetEuclidGCD } from './GetEuclidGCD'
// CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
/**
@ -26,15 +19,11 @@ const GetEuclidGCD = (arg1, arg2) => {
* @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`.
*/
const CoPrimeCheck = (firstNumber, secondNumber) => {
// firstly, check that input is a number or not.
if (typeof firstNumber !== 'number' || typeof secondNumber !== '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(Math.abs(firstNumber), Math.abs(secondNumber)) === 1
return GetEuclidGCD(firstNumber, secondNumber) === 1
}
export { CoPrimeCheck }