From 48021e1df999008819db04ada6be366c7bb2b06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 27 Jul 2022 14:57:23 +0200 Subject: [PATCH 1/7] Fix GetEuclidGCD Implement the actual Euclidean Algorithm --- Maths/GetEuclidGCD.js | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index 3aff8dbbeb..8703a3979f 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -1,32 +1,19 @@ -/* - Problem statement and Explanation : https://en.wikipedia.org/wiki/Euclidean_algorithm - - In this method, we have followed the iterative approach to first - find a minimum of both numbers and go to the next step. -*/ - /** - * GetEuclidGCD return the gcd of two numbers using Euclidean algorithm. - * @param {Number} arg1 first argument for gcd - * @param {Number} arg2 second argument for gcd - * @returns return a `gcd` value of both number. + * 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) */ -const GetEuclidGCD = (arg1, arg2) => { - // firstly, check that input is a number or not. - if (typeof arg1 !== 'number' || typeof arg2 !== 'number') { - return new TypeError('Argument is not a number.') +export function GetEuclidGCD(a, b) { + if (typeof a !== 'number' || typeof b !== 'number') { + throw new TypeError('Arguments must be numbers') } - // check that the input number is not a negative value. - if (arg1 < 1 || arg2 < 1) { - return new TypeError('Argument is a negative number.') + a, b = Math.abs(a), Math.abs(b) + if (a == 0 && b == 0) return undefined // infinitely many numbers divide 0 + if (a < 0) a = -a + if (b < 0) b = -b + while (b !== 0) { + a, b = b, a % b } - // Find a minimum of both numbers. - let less = arg1 > arg2 ? arg2 : arg1 - // Iterate the number and find the gcd of the number using the above explanation. - for (less; less >= 2; less--) { - if ((arg1 % less === 0) && (arg2 % less === 0)) return (less) - } - return (less) + return b } - -export { GetEuclidGCD } From 5d5e4e878f6c930687add49238b629ba1330a445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:01:44 +0200 Subject: [PATCH 2/7] Replace == with === --- Maths/GetEuclidGCD.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index 8703a3979f..06e8c9c069 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -9,7 +9,7 @@ export function GetEuclidGCD(a, b) { throw new TypeError('Arguments must be numbers') } a, b = Math.abs(a), Math.abs(b) - if (a == 0 && b == 0) return undefined // infinitely many numbers divide 0 + if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 if (a < 0) a = -a if (b < 0) b = -b while (b !== 0) { From 734d1058f97306747753d79ac6e72bc6461f9e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:04:19 +0200 Subject: [PATCH 3/7] Lua > JS --- Maths/GetEuclidGCD.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index 06e8c9c069..6cda4ab4d5 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -8,12 +8,15 @@ export function GetEuclidGCD(a, b) { if (typeof a !== 'number' || typeof b !== 'number') { throw new TypeError('Arguments must be numbers') } - a, b = Math.abs(a), Math.abs(b) + a = Math.abs(a) + b = Math.abs(b) if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 if (a < 0) a = -a if (b < 0) b = -b while (b !== 0) { - a, b = b, a % b + const rem = a % b + a = b + b = rem } return b } From 9815d71a46f3dac34b81a8ebeeb0940cc3cdbf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:06:17 +0200 Subject: [PATCH 4/7] Standard sucks --- Maths/GetEuclidGCD.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index 6cda4ab4d5..be915a1706 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -4,7 +4,7 @@ * @param {Number} b integer (may be negative) * @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') { throw new TypeError('Arguments must be numbers') } From 071571b82cd3889fc8ed1139af0b7451d895e46a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:09:27 +0200 Subject: [PATCH 5/7] Oops --- Maths/GetEuclidGCD.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index be915a1706..0a2c6b642e 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -18,5 +18,5 @@ export function GetEuclidGCD (a, b) { a = b b = rem } - return b + return a } From b165ca34559a344890f8aa3829b0e113828308da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:10:01 +0200 Subject: [PATCH 6/7] Update GetEuclidGCD.js --- Maths/GetEuclidGCD.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index 0a2c6b642e..aa77acbcc1 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -8,11 +8,9 @@ export function GetEuclidGCD (a, b) { if (typeof a !== 'number' || typeof b !== 'number') { throw new TypeError('Arguments must be numbers') } + if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 a = Math.abs(a) b = Math.abs(b) - if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 - if (a < 0) a = -a - if (b < 0) b = -b while (b !== 0) { const rem = a % b a = b From bd4708480b488aa40cc2c9277a69097d0b328013 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 27 Jul 2022 13:10:35 +0000 Subject: [PATCH 7/7] Updated Documentation in README.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3793104e27..5a491bf570 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -18,6 +18,7 @@ * **Cellular-Automata** * [ConwaysGameOfLife](Cellular-Automata/ConwaysGameOfLife.js) * **Ciphers** + * [AffineCipher](Ciphers/AffineCipher.js) * [Atbash](Ciphers/Atbash.js) * [CaesarsCipher](Ciphers/CaesarsCipher.js) * [KeyFinder](Ciphers/KeyFinder.js)