Skip to content

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