euclidean algorithm in crystal#887
Open
henrikac wants to merge 1 commit intoalgorithm-archivists:mainfrom
Open
euclidean algorithm in crystal#887henrikac wants to merge 1 commit intoalgorithm-archivists:mainfrom
henrikac wants to merge 1 commit intoalgorithm-archivists:mainfrom
Conversation
Contributor
Author
|
[lang: crystal] |
Andriamanitra
suggested changes
Apr 7, 2022
| b = b.abs | ||
|
|
||
| loop do | ||
| b, a = a % b, b |
There was a problem hiding this comment.
If b is already zero trying to do a % b throws DivisionByZeroError. This could be fixed by breaking before this line, or using while instead of loop
Comment on lines
+17
to
+24
| loop do | ||
| if a > b | ||
| a -= b | ||
| else | ||
| b -= a | ||
| end | ||
| break if a == b | ||
| end |
There was a problem hiding this comment.
This goes into an infinite loop if a is equal to b. Moving the break to happen before the if would fix the issue, but I think even better would be to use a while loop instead:
while a != b
if a > b
a -= b
else
b -= a
end
end
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The PR adds example of how to implement euclidean algorithm in Crystal.