From d79e2f71faadcee3f3df40ddcaf951d76393b1d2 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 7 Sep 2021 03:10:17 +0530 Subject: [PATCH 1/6] add CoPrimeCheck method --- Maths/CoPrimeCheck.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Maths/CoPrimeCheck.js diff --git a/Maths/CoPrimeCheck.js b/Maths/CoPrimeCheck.js new file mode 100644 index 000000000..1bc3d32ce --- /dev/null +++ b/Maths/CoPrimeCheck.js @@ -0,0 +1,30 @@ +/* + Problem statement and Explanation : https://en.wikipedia.org/wiki/Coprime_integers + + In number theory, two integers a and b are coprime, relatively prime or + mutually prime if the only positive integer that is a divisor of both + of them is Consequently, any prime number that divides one of a + or b does not divide the other. This is equivalent to their greatest + common divisor (gcd) being. One says also a is prime to b or a + is coprime with b. +*/ + +// Here we require an already implemented method. +const GetEuclidGCD = require('./GetEuclidGCD') + +// CoPrimeCheck function return the boolean in respect of the given number is co-prime or not. +/** + * CoPrimeCheck function return the boolean in respect of the given number is co-prime or not. + * @param {Number} firstNumber first number for checking is prime or not. + * @param {Number} secondNumber second number for checking is prime or not. + * @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`. + */ +const CoPrimeCheck = (firstNumber, secondNumber) => { + /* + This is the most efficient algorithm for checking co-primes + if the GCD of both the numbers is 1 that means they are co-primes. + */ + return GetEuclidGCD(firstNumber, secondNumber) === 1 +} + +module.exports = CoPrimeCheck From 617ec6ba27cc6c4c4a833d94e5f4f429b26aa42d Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 7 Sep 2021 03:23:02 +0530 Subject: [PATCH 2/6] fix number checking --- Maths/CoPrimeCheck.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Maths/CoPrimeCheck.js b/Maths/CoPrimeCheck.js index 1bc3d32ce..f85cf8906 100644 --- a/Maths/CoPrimeCheck.js +++ b/Maths/CoPrimeCheck.js @@ -20,6 +20,10 @@ const GetEuclidGCD = require('./GetEuclidGCD') * @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`. */ const CoPrimeCheck = (firstNumber, secondNumber) => { + // firstly, check that input is a number or not. + if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') { + return new TypeError('Argument is not a number.'); + } /* This is the most efficient algorithm for checking co-primes if the GCD of both the numbers is 1 that means they are co-primes. From 7fb345b4bd76be9476ce3c87d6d132a8d5ce5331 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 7 Sep 2021 03:23:38 +0530 Subject: [PATCH 3/6] fix typing style --- Maths/CoPrimeCheck.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/CoPrimeCheck.js b/Maths/CoPrimeCheck.js index f85cf8906..b2fd848f4 100644 --- a/Maths/CoPrimeCheck.js +++ b/Maths/CoPrimeCheck.js @@ -20,10 +20,10 @@ const GetEuclidGCD = require('./GetEuclidGCD') * @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`. */ const CoPrimeCheck = (firstNumber, secondNumber) => { - // firstly, check that input is a number or not. - if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') { - return new TypeError('Argument is not a number.'); - } + // firstly, check that input is a number or not. + if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') { + return new TypeError('Argument is not a number.') + } /* This is the most efficient algorithm for checking co-primes if the GCD of both the numbers is 1 that means they are co-primes. From 800308f21602fdf6de4dc6dd0a806c5320822961 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 7 Sep 2021 03:33:17 +0530 Subject: [PATCH 4/6] add CheckKishnamurthyNumber --- Maths/CheckKishnamurthyNumber.js | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Maths/CheckKishnamurthyNumber.js diff --git a/Maths/CheckKishnamurthyNumber.js b/Maths/CheckKishnamurthyNumber.js new file mode 100644 index 000000000..ab512bfb8 --- /dev/null +++ b/Maths/CheckKishnamurthyNumber.js @@ -0,0 +1,40 @@ +/* + Problem statement and Explanation : https://www.geeksforgeeks.org/check-if-a-number-is-a-krishnamurthy-number-or-not-2/ + + krishnamurthy number is a number the sum of the all fectorial of the all dights is equal to the number itself. + 145 => 1! + 4! + 5! = 1 + 24 + 120 = 145 +*/ + +// factorail utility method. +const factorial = (n) => { + let fact = 1 + while (n !== 0) { + fact = fact * n + n-- + } + return fact +} + +/** + * krishnamurthy number is a number the sum of the factorial of the all dights is equal to the number itself. + * @param {Number} number a number for checking is krishnamurthy number or not. + * @returns return correspond boolean vlaue, if the number is krishnamurthy number return `true` else return `false`. + * @example 145 => 1! + 4! + 5! = 1 + 24 + 120 = 145 + */ +const CheckKishnamurthyNumber = (number) => { + // firstly, check that input is a number or not. + if (typeof number !== 'number') { + return new TypeError('Argument is not a number.') + } + // create a variable to store the sum of all digits factorial. + let sumOfAllDigitFactorial = 0 + // convert the number to string for convenience. + String(number).split('').map(digit => { + // split one by one digit and calculate factorial and store to the variable. + return (sumOfAllDigitFactorial += factorial(Number(digit))) + }) + // if the sumOftheFactorial is equal to the given number it means the number is a Krishnamurthy number. + return sumOfAllDigitFactorial === number +} + +module.exports = CheckKishnamurthyNumber From 5a8bb6712442e460495dc0a7bda3f0eb5a6eb7ba Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 7 Sep 2021 17:55:11 +0530 Subject: [PATCH 5/6] fix the string method problem --- Maths/CheckKishnamurthyNumber.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Maths/CheckKishnamurthyNumber.js b/Maths/CheckKishnamurthyNumber.js index ab512bfb8..bb283e68e 100644 --- a/Maths/CheckKishnamurthyNumber.js +++ b/Maths/CheckKishnamurthyNumber.js @@ -29,10 +29,14 @@ const CheckKishnamurthyNumber = (number) => { // create a variable to store the sum of all digits factorial. let sumOfAllDigitFactorial = 0 // convert the number to string for convenience. - String(number).split('').map(digit => { - // split one by one digit and calculate factorial and store to the variable. - return (sumOfAllDigitFactorial += factorial(Number(digit))) - }) + let newNumber = number + // Extract number digits using the remainder method. + while (newNumber > 0) { + const lastDigit = newNumber % 10 + // calculate each digit factorial. + sumOfAllDigitFactorial += factorial(lastDigit) + newNumber = Math.floor(newNumber / 10) + } // if the sumOftheFactorial is equal to the given number it means the number is a Krishnamurthy number. return sumOfAllDigitFactorial === number } From 5e3106e81be4340c8f280e1da464e3f79398c0db Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 7 Sep 2021 18:10:47 +0530 Subject: [PATCH 6/6] fix, self-contained gcd method --- Maths/CoPrimeCheck.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Maths/CoPrimeCheck.js b/Maths/CoPrimeCheck.js index b2fd848f4..11aa7b89b 100644 --- a/Maths/CoPrimeCheck.js +++ b/Maths/CoPrimeCheck.js @@ -9,8 +9,14 @@ is coprime with b. */ -// Here we require an already implemented method. -const GetEuclidGCD = require('./GetEuclidGCD') +// Here we use a GetEuclidGCD method as a utility. +const GetEuclidGCD = (arg1, arg2) => { + let less = arg1 > arg2 ? arg2 : arg1 + for (less; less >= 2; less--) { + if ((arg1 % less === 0) && (arg2 % less === 0)) return (less) + } + return (less) +} // CoPrimeCheck function return the boolean in respect of the given number is co-prime or not. /**