| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
[lang: crystal] |
Sorry, something went wrong.
| 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
Sorry, something went wrong.
| 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
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
The PR adds example of how to implement euclidean algorithm in Crystal.