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:
Piotr Idzik
2024-03-16 04:59:14 +01:00
committed by GitHub
parent 0204198465
commit bd34e9fa61
4 changed files with 46 additions and 55 deletions

View File

@ -260,7 +260,6 @@
* [SquareRootLogarithmic](Maths/SquareRootLogarithmic.js)
* [SumOfDigits](Maths/SumOfDigits.js)
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
* [TwinPrime](Maths/TwinPrime.js)
* [TwoSum](Maths/TwoSum.js)
* [Volume](Maths/Volume.js)
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
@ -296,7 +295,6 @@
* **Recursive**
* [BinaryEquivalent](Recursive/BinaryEquivalent.js)
* [BinarySearch](Recursive/BinarySearch.js)
* [EucledianGCD](Recursive/EucledianGCD.js)
* [Factorial](Recursive/Factorial.js)
* [FibonacciNumberRecursive](Recursive/FibonacciNumberRecursive.js)
* [FloodFill](Recursive/FloodFill.js)

View File

@ -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
* @param {Number} a integer (may be negative)
@ -5,9 +11,7 @@
* @returns {Number} Greatest Common Divisor gcd(a, b)
*/
export function GetEuclidGCD(a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw new TypeError('Arguments must be numbers')
}
CheckInput(a, b)
a = Math.abs(a)
b = Math.abs(b)
while (b !== 0) {
@ -17,3 +21,19 @@ export function GetEuclidGCD(a, b) {
}
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)
}

View File

@ -1,22 +1,25 @@
import { GetEuclidGCD } from '../GetEuclidGCD'
import { GetEuclidGCD, GetEuclidGCDRecursive } from '../GetEuclidGCD'
describe('GetEuclidGCD', () => {
it.each([
[5, 20, 5],
[109, 902, 1],
[290, 780, 10],
[104, 156, 52],
[0, 100, 100],
[-5, 50, 5],
[0, 0, 0],
[1, 1234567, 1]
])('returns correct result for %i and %j', (inputA, inputB, expected) => {
expect(GetEuclidGCD(inputA, inputB)).toBe(expected)
expect(GetEuclidGCD(inputB, inputA)).toBe(expected)
})
describe.each([GetEuclidGCD, GetEuclidGCDRecursive])(
'%# GetEuclidGCD',
(gcdFunction) => {
it.each([
[5, 20, 5],
[109, 902, 1],
[290, 780, 10],
[104, 156, 52],
[0, 100, 100],
[-5, 50, 5],
[0, 0, 0],
[1, 1234567, 1]
])('returns correct result for %i and %j', (inputA, inputB, expected) => {
expect(gcdFunction(inputA, inputB)).toBe(expected)
expect(gcdFunction(inputB, inputA)).toBe(expected)
})
it('should throw when any of the inputs is not a number', () => {
expect(() => GetEuclidGCD('1', 2)).toThrowError()
expect(() => GetEuclidGCD(1, '2')).toThrowError()
})
})
it('should throw when any of the inputs is not a number', () => {
expect(() => gcdFunction('1', 2)).toThrowError()
expect(() => gcdFunction(1, '2')).toThrowError()
})
}
)

View File

@ -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 }