FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

euclidean algorithm in crystal by henrikac · Pull Request #887 · algorithm-archivists/algorithm-archive · GitHub

euclidean algorithm in crystal - #887

Open
henrikac wants to merge 1 commit into
algorithm-archivists:mainfrom
henrikac:euclidean-crystal
Open

euclidean algorithm in crystal#887
henrikac wants to merge 1 commit into
algorithm-archivists:mainfrom
henrikac:euclidean-crystal

Conversation

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

[lang: crystal]

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

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


Back | FazBrowse Home | New Git URL