mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
fix: handle zeros in CoPrimeCheck
(#1622)
This commit is contained in:
@ -9,14 +9,7 @@
|
|||||||
is coprime with b.
|
is coprime with b.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Here we use a GetEuclidGCD method as a utility.
|
import { GetEuclidGCD } from './GetEuclidGCD'
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
|
// 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`.
|
* @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`.
|
||||||
*/
|
*/
|
||||||
const CoPrimeCheck = (firstNumber, secondNumber) => {
|
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
|
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.
|
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 }
|
export { CoPrimeCheck }
|
||||||
|
@ -8,7 +8,9 @@ describe('CoPrimeCheck', () => {
|
|||||||
[1, 7],
|
[1, 7],
|
||||||
[20, 21],
|
[20, 21],
|
||||||
[5, 7],
|
[5, 7],
|
||||||
[-5, -7]
|
[-5, -7],
|
||||||
|
[1, 0],
|
||||||
|
[-1, 0]
|
||||||
])('returns true for %j and %i', (inputA, inputB) => {
|
])('returns true for %j and %i', (inputA, inputB) => {
|
||||||
expect(CoPrimeCheck(inputA, inputB)).toBe(true)
|
expect(CoPrimeCheck(inputA, inputB)).toBe(true)
|
||||||
expect(CoPrimeCheck(inputB, inputA)).toBe(true)
|
expect(CoPrimeCheck(inputB, inputA)).toBe(true)
|
||||||
@ -16,7 +18,9 @@ describe('CoPrimeCheck', () => {
|
|||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
[5, 15],
|
[5, 15],
|
||||||
[13 * 17 * 19, 17 * 23 * 29]
|
[13 * 17 * 19, 17 * 23 * 29],
|
||||||
|
[2, 0],
|
||||||
|
[0, 0]
|
||||||
])('returns false for %j and %i', (inputA, inputB) => {
|
])('returns false for %j and %i', (inputA, inputB) => {
|
||||||
expect(CoPrimeCheck(inputA, inputB)).toBe(false)
|
expect(CoPrimeCheck(inputA, inputB)).toBe(false)
|
||||||
expect(CoPrimeCheck(inputB, inputA)).toBe(false)
|
expect(CoPrimeCheck(inputB, inputA)).toBe(false)
|
||||||
|
Reference in New Issue
Block a user