mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
feat: remove duplicated gcd
-like functions (#1642)
* feat: remove duplicated `gcd`-like functions * Updated Documentation in README.md --------- Co-authored-by: vil02 <vil02@users.noreply.github.com>
This commit is contained in:
@ -260,7 +260,6 @@
|
|||||||
* [SquareRootLogarithmic](Maths/SquareRootLogarithmic.js)
|
* [SquareRootLogarithmic](Maths/SquareRootLogarithmic.js)
|
||||||
* [SumOfDigits](Maths/SumOfDigits.js)
|
* [SumOfDigits](Maths/SumOfDigits.js)
|
||||||
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
|
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
|
||||||
* [TwinPrime](Maths/TwinPrime.js)
|
|
||||||
* [TwoSum](Maths/TwoSum.js)
|
* [TwoSum](Maths/TwoSum.js)
|
||||||
* [Volume](Maths/Volume.js)
|
* [Volume](Maths/Volume.js)
|
||||||
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
|
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
|
||||||
@ -296,7 +295,6 @@
|
|||||||
* **Recursive**
|
* **Recursive**
|
||||||
* [BinaryEquivalent](Recursive/BinaryEquivalent.js)
|
* [BinaryEquivalent](Recursive/BinaryEquivalent.js)
|
||||||
* [BinarySearch](Recursive/BinarySearch.js)
|
* [BinarySearch](Recursive/BinarySearch.js)
|
||||||
* [EucledianGCD](Recursive/EucledianGCD.js)
|
|
||||||
* [Factorial](Recursive/Factorial.js)
|
* [Factorial](Recursive/Factorial.js)
|
||||||
* [FibonacciNumberRecursive](Recursive/FibonacciNumberRecursive.js)
|
* [FibonacciNumberRecursive](Recursive/FibonacciNumberRecursive.js)
|
||||||
* [FloodFill](Recursive/FloodFill.js)
|
* [FloodFill](Recursive/FloodFill.js)
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
function CheckInput(a, b) {
|
||||||
|
if (typeof a !== 'number' || typeof b !== 'number') {
|
||||||
|
throw new TypeError('Arguments must be numbers')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers
|
* GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers
|
||||||
* @param {Number} a integer (may be negative)
|
* @param {Number} a integer (may be negative)
|
||||||
@ -5,9 +11,7 @@
|
|||||||
* @returns {Number} Greatest Common Divisor gcd(a, b)
|
* @returns {Number} Greatest Common Divisor gcd(a, b)
|
||||||
*/
|
*/
|
||||||
export function GetEuclidGCD(a, b) {
|
export function GetEuclidGCD(a, b) {
|
||||||
if (typeof a !== 'number' || typeof b !== 'number') {
|
CheckInput(a, b)
|
||||||
throw new TypeError('Arguments must be numbers')
|
|
||||||
}
|
|
||||||
a = Math.abs(a)
|
a = Math.abs(a)
|
||||||
b = Math.abs(b)
|
b = Math.abs(b)
|
||||||
while (b !== 0) {
|
while (b !== 0) {
|
||||||
@ -17,3 +21,19 @@ export function GetEuclidGCD(a, b) {
|
|||||||
}
|
}
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursive version of GetEuclidGCD
|
||||||
|
* @param {Number} a integer (may be negative)
|
||||||
|
* @param {Number} b integer (may be negative)
|
||||||
|
* @returns {Number} Greatest Common Divisor gcd(a, b)
|
||||||
|
*/
|
||||||
|
export function GetEuclidGCDRecursive(a, b) {
|
||||||
|
CheckInput(a, b)
|
||||||
|
a = Math.abs(a)
|
||||||
|
b = Math.abs(b)
|
||||||
|
if (b == 0) {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return GetEuclidGCDRecursive(b, a % b)
|
||||||
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { GetEuclidGCD } from '../GetEuclidGCD'
|
import { GetEuclidGCD, GetEuclidGCDRecursive } from '../GetEuclidGCD'
|
||||||
|
|
||||||
describe('GetEuclidGCD', () => {
|
describe.each([GetEuclidGCD, GetEuclidGCDRecursive])(
|
||||||
|
'%# GetEuclidGCD',
|
||||||
|
(gcdFunction) => {
|
||||||
it.each([
|
it.each([
|
||||||
[5, 20, 5],
|
[5, 20, 5],
|
||||||
[109, 902, 1],
|
[109, 902, 1],
|
||||||
@ -11,12 +13,13 @@ describe('GetEuclidGCD', () => {
|
|||||||
[0, 0, 0],
|
[0, 0, 0],
|
||||||
[1, 1234567, 1]
|
[1, 1234567, 1]
|
||||||
])('returns correct result for %i and %j', (inputA, inputB, expected) => {
|
])('returns correct result for %i and %j', (inputA, inputB, expected) => {
|
||||||
expect(GetEuclidGCD(inputA, inputB)).toBe(expected)
|
expect(gcdFunction(inputA, inputB)).toBe(expected)
|
||||||
expect(GetEuclidGCD(inputB, inputA)).toBe(expected)
|
expect(gcdFunction(inputB, inputA)).toBe(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw when any of the inputs is not a number', () => {
|
it('should throw when any of the inputs is not a number', () => {
|
||||||
expect(() => GetEuclidGCD('1', 2)).toThrowError()
|
expect(() => gcdFunction('1', 2)).toThrowError()
|
||||||
expect(() => GetEuclidGCD(1, '2')).toThrowError()
|
expect(() => gcdFunction(1, '2')).toThrowError()
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
function euclideanGCDRecursive(first, second) {
|
|
||||||
/*
|
|
||||||
Calculates GCD of two numbers using Euclidean Recursive Algorithm
|
|
||||||
:param first: First number
|
|
||||||
:param second: Second number
|
|
||||||
:return: GCD of the numbers
|
|
||||||
*/
|
|
||||||
if (second === 0) {
|
|
||||||
return first
|
|
||||||
} else {
|
|
||||||
return euclideanGCDRecursive(second, first % second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function euclideanGCDIterative(first, second) {
|
|
||||||
/*
|
|
||||||
Calculates GCD of two numbers using Euclidean Iterative Algorithm
|
|
||||||
:param first: First number
|
|
||||||
:param second: Second number
|
|
||||||
:return: GCD of the numbers
|
|
||||||
*/
|
|
||||||
while (second !== 0) {
|
|
||||||
const temp = second
|
|
||||||
second = first % second
|
|
||||||
first = temp
|
|
||||||
}
|
|
||||||
return first
|
|
||||||
}
|
|
||||||
|
|
||||||
export { euclideanGCDIterative, euclideanGCDRecursive }
|
|
Reference in New Issue
Block a user