mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
chore: Merge pull request #682 from suryapratapsinghsuryavanshi/master
added CoPrimeCheck and CheckKishnamurthyNumber methods
This commit is contained in:
44
Maths/CheckKishnamurthyNumber.js
Normal file
44
Maths/CheckKishnamurthyNumber.js
Normal file
@ -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
|
40
Maths/CoPrimeCheck.js
Normal file
40
Maths/CoPrimeCheck.js
Normal file
@ -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
|
Reference in New Issue
Block a user