From 10373dda7be618bf64f282fc1b1e2ad7f812b35d Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 31 Aug 2021 03:31:53 +0530 Subject: [PATCH 1/6] add GetGCD method --- Maths/GetGCD.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Maths/GetGCD.js diff --git a/Maths/GetGCD.js b/Maths/GetGCD.js new file mode 100644 index 0000000000..1b677ddc44 --- /dev/null +++ b/Maths/GetGCD.js @@ -0,0 +1,26 @@ +/* + Problem statement and Explanation : https://en.wikipedia.org/wiki/Greatest_common_divisor + + In this method, we have followed the iterative approach to first + find a minimum of both numbers and go to the next step. +*/ + +/** + * GetGCD return the gcd of two numbers. + * @param {Number} arg1 first argument for gcd + * @param {Number} arg2 second argument for gcd + * @returns return a `gcd` value of both number. + */ +const getGcd = (arg1, arg2) => { + // 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) +} + +module.exports = getGcd From 60b185d8cb595531889cdcf5d3dc6d53a741eebd Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 31 Aug 2021 03:32:44 +0530 Subject: [PATCH 2/6] re-formate GetGCD with standard.js --- Maths/GetGCD.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Maths/GetGCD.js b/Maths/GetGCD.js index 1b677ddc44..a9090368a8 100644 --- a/Maths/GetGCD.js +++ b/Maths/GetGCD.js @@ -12,15 +12,15 @@ * @returns return a `gcd` value of both number. */ const getGcd = (arg1, arg2) => { - // 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) - } + // Find a minimum of both numbers. - return (less) + 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) } module.exports = getGcd From d7e8bf492d039f3730541708f72b87ab269288e4 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 31 Aug 2021 03:33:35 +0530 Subject: [PATCH 3/6] fix types error in GetGCD method --- Maths/GetGCD.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Maths/GetGCD.js b/Maths/GetGCD.js index a9090368a8..0efa695edf 100644 --- a/Maths/GetGCD.js +++ b/Maths/GetGCD.js @@ -13,7 +13,6 @@ */ const getGcd = (arg1, arg2) => { // 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--) { From c67b0e9f1d031040cf362808e112fc79736c57ad Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 31 Aug 2021 03:53:53 +0530 Subject: [PATCH 4/6] add ReverseNumber method --- Maths/ReverseNumber.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Maths/ReverseNumber.js diff --git a/Maths/ReverseNumber.js b/Maths/ReverseNumber.js new file mode 100644 index 0000000000..d3e2695d8c --- /dev/null +++ b/Maths/ReverseNumber.js @@ -0,0 +1,25 @@ +/* + Problem statement and Explanation : https://medium.com/@ManBearPigCode/how-to-reverse-a-number-mathematically-97c556626ec6 +*/ + +/** + * ReverseNumber return the reversed value of the given number. + * @param {Number} n any digit number. + * @returns `Number` n reverse in reverse. + */ +const ReverseNumber = (number) => { + // A variable for storing the reversed number. + let reverseNumber = 0 + // Iterate the process until getting the number is 0. + while (number > 0) { + // get the last digit of the number + const lastDigit = number % 10 + // add to the last digit to in reverseNumber + reverseNumber = reverseNumber * 10 + lastDigit + // reduce the actual number. + number = Math.floor(number / 10) + } + return reverseNumber +} + +module.exports = ReverseNumber From 92b8b46f36689df437e0d2a3f5310773e50174af Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 31 Aug 2021 13:48:39 +0530 Subject: [PATCH 5/6] add type checking --- Maths/{GetGCD.js => GetEuclidGCD.js} | 6 +++++- Maths/ReverseNumber.js | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) rename Maths/{GetGCD.js => GetEuclidGCD.js} (78%) diff --git a/Maths/GetGCD.js b/Maths/GetEuclidGCD.js similarity index 78% rename from Maths/GetGCD.js rename to Maths/GetEuclidGCD.js index 0efa695edf..8629999612 100644 --- a/Maths/GetGCD.js +++ b/Maths/GetEuclidGCD.js @@ -1,5 +1,5 @@ /* - Problem statement and Explanation : https://en.wikipedia.org/wiki/Greatest_common_divisor + 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. @@ -12,6 +12,10 @@ * @returns return a `gcd` value of both number. */ const getGcd = (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.') + } // 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. diff --git a/Maths/ReverseNumber.js b/Maths/ReverseNumber.js index d3e2695d8c..4995761f3c 100644 --- a/Maths/ReverseNumber.js +++ b/Maths/ReverseNumber.js @@ -8,6 +8,10 @@ * @returns `Number` n reverse in reverse. */ const ReverseNumber = (number) => { + // firstly, check that input is a number or not. + if (typeof number !== 'number') { + return new TypeError('Argument is not a number.') + } // A variable for storing the reversed number. let reverseNumber = 0 // Iterate the process until getting the number is 0. From 09ce0c76a66dd207c0ab6c3bdfe134d4194096ff Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 31 Aug 2021 13:53:49 +0530 Subject: [PATCH 6/6] change the GetGCD method to GetEuclidGCD method --- Maths/GetEuclidGCD.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Maths/GetEuclidGCD.js b/Maths/GetEuclidGCD.js index 8629999612..5eb51597b7 100644 --- a/Maths/GetEuclidGCD.js +++ b/Maths/GetEuclidGCD.js @@ -6,24 +6,27 @@ */ /** - * GetGCD return the gcd of two numbers. + * 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. */ -const getGcd = (arg1, arg2) => { +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.') } + // check that the input number is not a negative value. + if (arg1 < 1 || arg2 < 1) { + return new TypeError('Argument is a negative number.') + } // 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) } -module.exports = getGcd +module.exports = GetEuclidGCD