Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 23 additions & 3 deletions Maths/GetEuclidGCD.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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)
* @param {Number} b integer (may be negative)
* @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) {
Expand All @@ -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)
}
43 changes: 23 additions & 20 deletions Maths/test/GetEuclidGCD.test.js
Original file line number Diff line number Diff line change
@@ -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()
})
}
)
30 changes: 0 additions & 30 deletions Recursive/EucledianGCD.js

This file was deleted.