Skip to content

Commit bd34e9f

Browse files
authored
feat: remove duplicated gcd-like functions (TheAlgorithms#1642)
* feat: remove duplicated `gcd`-like functions * Updated Documentation in README.md --------- Co-authored-by: vil02 <[email protected]>
1 parent 0204198 commit bd34e9f

File tree

4 files changed

+46
-55
lines changed

4 files changed

+46
-55
lines changed

‎DIRECTORY.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@
260260
*[SquareRootLogarithmic](Maths/SquareRootLogarithmic.js)
261261
*[SumOfDigits](Maths/SumOfDigits.js)
262262
*[SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
263-
*[TwinPrime](Maths/TwinPrime.js)
264263
*[TwoSum](Maths/TwoSum.js)
265264
*[Volume](Maths/Volume.js)
266265
*[WhileLoopFactorial](Maths/WhileLoopFactorial.js)
@@ -296,7 +295,6 @@
296295
***Recursive**
297296
*[BinaryEquivalent](Recursive/BinaryEquivalent.js)
298297
*[BinarySearch](Recursive/BinarySearch.js)
299-
*[EucledianGCD](Recursive/EucledianGCD.js)
300298
*[Factorial](Recursive/Factorial.js)
301299
*[FibonacciNumberRecursive](Recursive/FibonacciNumberRecursive.js)
302300
*[FloodFill](Recursive/FloodFill.js)

‎Maths/GetEuclidGCD.js‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
functionCheckInput(a,b){
2+
if(typeofa!=='number'||typeofb!=='number'){
3+
thrownewTypeError('Arguments must be numbers')
4+
}
5+
}
6+
17
/**
28
* GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers
39
* @param{Number} a integer (may be negative)
410
* @param{Number} b integer (may be negative)
511
* @returns{Number} Greatest Common Divisor gcd(a, b)
612
*/
713
exportfunctionGetEuclidGCD(a,b){
8-
if(typeofa!=='number'||typeofb!=='number'){
9-
thrownewTypeError('Arguments must be numbers')
10-
}
14+
CheckInput(a,b)
1115
a=Math.abs(a)
1216
b=Math.abs(b)
1317
while(b!==0){
@@ -17,3 +21,19 @@ export function GetEuclidGCD(a, b){
1721
}
1822
returna
1923
}
24+
25+
/**
26+
* Recursive version of GetEuclidGCD
27+
* @param{Number} a integer (may be negative)
28+
* @param{Number} b integer (may be negative)
29+
* @returns{Number} Greatest Common Divisor gcd(a, b)
30+
*/
31+
exportfunctionGetEuclidGCDRecursive(a,b){
32+
CheckInput(a,b)
33+
a=Math.abs(a)
34+
b=Math.abs(b)
35+
if(b==0){
36+
returna
37+
}
38+
returnGetEuclidGCDRecursive(b,a%b)
39+
}

‎Maths/test/GetEuclidGCD.test.js‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import{GetEuclidGCD}from'../GetEuclidGCD'
1+
import{GetEuclidGCD,GetEuclidGCDRecursive}from'../GetEuclidGCD'
22

3-
describe('GetEuclidGCD',()=>{
4-
it.each([
5-
[5,20,5],
6-
[109,902,1],
7-
[290,780,10],
8-
[104,156,52],
9-
[0,100,100],
10-
[-5,50,5],
11-
[0,0,0],
12-
[1,1234567,1]
13-
])('returns correct result for %i and %j',(inputA,inputB,expected)=>{
14-
expect(GetEuclidGCD(inputA,inputB)).toBe(expected)
15-
expect(GetEuclidGCD(inputB,inputA)).toBe(expected)
16-
})
3+
describe.each([GetEuclidGCD,GetEuclidGCDRecursive])(
4+
'%# GetEuclidGCD',
5+
(gcdFunction)=>{
6+
it.each([
7+
[5,20,5],
8+
[109,902,1],
9+
[290,780,10],
10+
[104,156,52],
11+
[0,100,100],
12+
[-5,50,5],
13+
[0,0,0],
14+
[1,1234567,1]
15+
])('returns correct result for %i and %j',(inputA,inputB,expected)=>{
16+
expect(gcdFunction(inputA,inputB)).toBe(expected)
17+
expect(gcdFunction(inputB,inputA)).toBe(expected)
18+
})
1719

18-
it('should throw when any of the inputs is not a number',()=>{
19-
expect(()=>GetEuclidGCD('1',2)).toThrowError()
20-
expect(()=>GetEuclidGCD(1,'2')).toThrowError()
21-
})
22-
})
20+
it('should throw when any of the inputs is not a number',()=>{
21+
expect(()=>gcdFunction('1',2)).toThrowError()
22+
expect(()=>gcdFunction(1,'2')).toThrowError()
23+
})
24+
}
25+
)

‎Recursive/EucledianGCD.js‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
(0)