Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8734dfc

Browse files
authored
fix: handle zeros inCoPrimeCheck (TheAlgorithms#1622)
1 parenta5945e3 commit8734dfc

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

‎Maths/CoPrimeCheck.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@
99
is coprime with b.
1010
*/
1111

12-
// Here we use a GetEuclidGCD method as a utility.
13-
constGetEuclidGCD=(arg1,arg2)=>{
14-
letless=arg1>arg2 ?arg2 :arg1
15-
for(less;less>=2;less--){
16-
if(arg1%less===0&&arg2%less===0)returnless
17-
}
18-
returnless
19-
}
12+
import{GetEuclidGCD}from'./GetEuclidGCD'
2013

2114
// CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
2215
/**
@@ -26,15 +19,11 @@ const GetEuclidGCD = (arg1, arg2) => {
2619
*@returns return correspond boolean value, if both number are co-prime return `true`, else return `false`.
2720
*/
2821
constCoPrimeCheck=(firstNumber,secondNumber)=>{
29-
// firstly, check that input is a number or not.
30-
if(typeoffirstNumber!=='number'||typeofsecondNumber!=='number'){
31-
thrownewTypeError('Argument is not a number.')
32-
}
3322
/*
3423
This is the most efficient algorithm for checking co-primes
3524
if the GCD of both the numbers is 1 that means they are co-primes.
3625
*/
37-
returnGetEuclidGCD(Math.abs(firstNumber),Math.abs(secondNumber))===1
26+
returnGetEuclidGCD(firstNumber,secondNumber)===1
3827
}
3928

4029
export{CoPrimeCheck}

‎Maths/test/CoPrimeCheck.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ describe('CoPrimeCheck', () => {
88
[1,7],
99
[20,21],
1010
[5,7],
11-
[-5,-7]
11+
[-5,-7],
12+
[1,0],
13+
[-1,0]
1214
])('returns true for %j and %i',(inputA,inputB)=>{
1315
expect(CoPrimeCheck(inputA,inputB)).toBe(true)
1416
expect(CoPrimeCheck(inputB,inputA)).toBe(true)
1517
})
1618

1719
it.each([
1820
[5,15],
19-
[13*17*19,17*23*29]
21+
[13*17*19,17*23*29],
22+
[2,0],
23+
[0,0]
2024
])('returns false for %j and %i',(inputA,inputB)=>{
2125
expect(CoPrimeCheck(inputA,inputB)).toBe(false)
2226
expect(CoPrimeCheck(inputB,inputA)).toBe(false)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp