Skip to content
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

euclidean algorithm in crystal #887

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

henrikac
Copy link
Contributor

The PR adds example of how to implement euclidean algorithm in Crystal.

@henrikac
Copy link
Contributor Author

[lang: crystal]

@Amaras Amaras added hacktoberfest-accepted Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) labels Oct 19, 2021
b = b.abs

loop do
b, a = a % b, b

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) lang: crystal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants