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

Commit0e0cf98

Browse files
authored
fix: cleanupCoPrimeCheck (TheAlgorithms#1609)
1 parentfb0a99c commit0e0cf98

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
constCoPrimeCheck=(firstNumber,secondNumber)=>{
2929
// firstly, check that input is a number or not.
3030
if(typeoffirstNumber!=='number'||typeofsecondNumber!=='number'){
31-
returnnewTypeError('Argument is not a number.')
31+
thrownewTypeError('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-
returnGetEuclidGCD(firstNumber,secondNumber)===1
37+
returnGetEuclidGCD(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)

[8]ページ先頭

©2009-2025 Movatter.jp