-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
raklaptudirm
merged 6 commits into
TheAlgorithms:master
from
suryapratapsinghsuryavanshi:master
Sep 9, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d79e2f7
add CoPrimeCheck method
suryapratapsinghsuryavanshi 617ec6b
fix number checking
suryapratapsinghsuryavanshi 7fb345b
fix typing style
suryapratapsinghsuryavanshi 800308f
add CheckKishnamurthyNumber
suryapratapsinghsuryavanshi 5a8bb67
fix the string method problem
suryapratapsinghsuryavanshi 5e3106e
fix, self-contained gcd method
suryapratapsinghsuryavanshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.