diff --git a/Maths/CheckKishnamurthyNumber.js b/Maths/CheckKishnamurthyNumber.js new file mode 100644 index 0000000000..bb283e68eb --- /dev/null +++ b/Maths/CheckKishnamurthyNumber.js @@ -0,0 +1,44 @@ +/* + 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. + 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 +} + +module.exports = CheckKishnamurthyNumber diff --git a/Maths/CoPrimeCheck.js b/Maths/CoPrimeCheck.js new file mode 100644 index 0000000000..11aa7b89b9 --- /dev/null +++ b/Maths/CoPrimeCheck.js @@ -0,0 +1,40 @@ +/* + 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 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. +/** + * 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) => { + // 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. + */ + return GetEuclidGCD(firstNumber, secondNumber) === 1 +} + +module.exports = CoPrimeCheck pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy