Skip to content

Commit 210accf

Browse files
authored
chore: Merge pull request TheAlgorithms#682 from suryapratapsinghsuryavanshi/master
added CoPrimeCheck and CheckKishnamurthyNumber methods
2 parents f04dec3 + 5e3106e commit 210accf

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Maths/CheckKishnamurthyNumber.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Problem statement and Explanation : https://www.geeksforgeeks.org/check-if-a-number-is-a-krishnamurthy-number-or-not-2/
3+
4+
krishnamurthy number is a number the sum of the all fectorial of the all dights is equal to the number itself.
5+
145 => 1! + 4! + 5! = 1 + 24 + 120 = 145
6+
*/
7+
8+
// factorail utility method.
9+
const factorial = (n) => {
10+
let fact = 1
11+
while (n !== 0) {
12+
fact = fact * n
13+
n--
14+
}
15+
return fact
16+
}
17+
18+
/**
19+
* krishnamurthy number is a number the sum of the factorial of the all dights is equal to the number itself.
20+
* @param {Number} number a number for checking is krishnamurthy number or not.
21+
* @returns return correspond boolean vlaue, if the number is krishnamurthy number return `true` else return `false`.
22+
* @example 145 => 1! + 4! + 5! = 1 + 24 + 120 = 145
23+
*/
24+
const CheckKishnamurthyNumber = (number) => {
25+
// firstly, check that input is a number or not.
26+
if (typeof number !== 'number') {
27+
return new TypeError('Argument is not a number.')
28+
}
29+
// create a variable to store the sum of all digits factorial.
30+
let sumOfAllDigitFactorial = 0
31+
// convert the number to string for convenience.
32+
let newNumber = number
33+
// Extract number digits using the remainder method.
34+
while (newNumber > 0) {
35+
const lastDigit = newNumber % 10
36+
// calculate each digit factorial.
37+
sumOfAllDigitFactorial += factorial(lastDigit)
38+
newNumber = Math.floor(newNumber / 10)
39+
}
40+
// if the sumOftheFactorial is equal to the given number it means the number is a Krishnamurthy number.
41+
return sumOfAllDigitFactorial === number
42+
}
43+
44+
module.exports = CheckKishnamurthyNumber

Maths/CoPrimeCheck.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Problem statement and Explanation : https://en.wikipedia.org/wiki/Coprime_integers
3+
4+
In number theory, two integers a and b are coprime, relatively prime or
5+
mutually prime if the only positive integer that is a divisor of both
6+
of them is Consequently, any prime number that divides one of a
7+
or b does not divide the other. This is equivalent to their greatest
8+
common divisor (gcd) being. One says also a is prime to b or a
9+
is coprime with b.
10+
*/
11+
12+
// Here we use a GetEuclidGCD method as a utility.
13+
const GetEuclidGCD = (arg1, arg2) => {
14+
let less = arg1 > arg2 ? arg2 : arg1
15+
for (less; less >= 2; less--) {
16+
if ((arg1 % less === 0) && (arg2 % less === 0)) return (less)
17+
}
18+
return (less)
19+
}
20+
21+
// CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
22+
/**
23+
* CoPrimeCheck function return the boolean in respect of the given number is co-prime or not.
24+
* @param {Number} firstNumber first number for checking is prime or not.
25+
* @param {Number} secondNumber second number for checking is prime or not.
26+
* @returns return correspond boolean value, if both number are co-prime return `true`, else return `false`.
27+
*/
28+
const CoPrimeCheck = (firstNumber, secondNumber) => {
29+
// firstly, check that input is a number or not.
30+
if (typeof firstNumber !== 'number' || typeof secondNumber !== 'number') {
31+
return new TypeError('Argument is not a number.')
32+
}
33+
/*
34+
This is the most efficient algorithm for checking co-primes
35+
if the GCD of both the numbers is 1 that means they are co-primes.
36+
*/
37+
return GetEuclidGCD(firstNumber, secondNumber) === 1
38+
}
39+
40+
module.exports = CoPrimeCheck

0 commit comments

Comments
 (0)
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