-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Fix GetEuclidGCD #1068
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
Fix GetEuclidGCD #1068
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48021e1
Fix GetEuclidGCD
appgurueu 5d5e4e8
Replace == with ===
appgurueu 734d105
Lua > JS
appgurueu 9815d71
Standard sucks
appgurueu 071571b
Oops
appgurueu b165ca3
Update GetEuclidGCD.js
appgurueu bd47084
Updated Documentation in README.md
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
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 |
---|---|---|
@@ -1,32 +1,20 @@ | ||
/* | ||
Problem statement and Explanation : https://en.wikipedia.org/wiki/Euclidean_algorithm | ||
In this method, we have followed the iterative approach to first | ||
find a minimum of both numbers and go to the next step. | ||
*/ | ||
|
||
/** | ||
* GetEuclidGCD return the gcd of two numbers using Euclidean algorithm. | ||
* @param {Number} arg1 first argument for gcd | ||
* @param {Number} arg2 second argument for gcd | ||
* @returns return a `gcd` value of both number. | ||
* GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers | ||
* @param {Number} a integer (may be negative) | ||
* @param {Number} b integer (may be negative) | ||
* @returns {Number} Greatest Common Divisor gcd(a, b) | ||
*/ | ||
const GetEuclidGCD = (arg1, arg2) => { | ||
// firstly, check that input is a number or not. | ||
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') { | ||
return new TypeError('Argument is not a number.') | ||
export function GetEuclidGCD (a, b) { | ||
if (typeof a !== 'number' || typeof b !== 'number') { | ||
throw new TypeError('Arguments must be numbers') | ||
} | ||
// check that the input number is not a negative value. | ||
if (arg1 < 1 || arg2 < 1) { | ||
return new TypeError('Argument is a negative number.') | ||
if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 | ||
a = Math.abs(a) | ||
b = Math.abs(b) | ||
while (b !== 0) { | ||
const rem = a % b | ||
a = b | ||
b = rem | ||
} | ||
// Find a minimum of both numbers. | ||
let less = arg1 > arg2 ? arg2 : arg1 | ||
// Iterate the number and find the gcd of the number using the above explanation. | ||
for (less; less >= 2; less--) { | ||
if ((arg1 % less === 0) && (arg2 % less === 0)) return (less) | ||
} | ||
return (less) | ||
return a | ||
} | ||
|
||
export { GetEuclidGCD } |
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.