Skip to content

added CoPrimeCheck and CheckKishnamurthyNumber methods #682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Maths/CheckKishnamurthyNumber.js
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions Maths/CoPrimeCheck.js
Original file line number Diff line number Diff line change
@@ -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