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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions contents/euclidean_algorithm/code/crystal/euclidean.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def euclid_mod(a : Int, b : Int)
a = a.abs
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

break if b == 0
end

return a
end

def euclid_sub(a : Int, b : Int)
a = a.abs
b = b.abs

loop do
if a > b
a -= b
else
b -= a
end
break if a == b
end
Comment on lines +17 to +24

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


return a
end

def main
euc_mod = euclid_mod(64 * 67, 64 * 81)
euc_sub = euclid_sub(128 * 12, 128 * 77)

puts "Modulus-based euclidean algorithm result: #{euc_mod}"
puts "Subtraction-based euclidean algorithm result: #{euc_sub}"
end

main
6 changes: 6 additions & 0 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
[import:1-14, lang="powershell"](code/powershell/euclidean_algorithm.ps1)
{% sample lang="coco" %}
[import:1-9, lang="coconut"](code/coconut/euclidean.coco)
{% sample lang="crystal" %}
[import:13-27, lang="crystal"](code/crystal/euclidean.cr)

{% endmethod %}

Expand Down Expand Up @@ -173,6 +175,8 @@ Modern implementations, though, often use the modulus operator (%) like so
[import:16-27, lang="powershell"](code/powershell/euclidean_algorithm.ps1)
{% sample lang="coco" %}
[import:12-15, lang="coconut"](code/coconut/euclidean.coco)
{% sample lang="crystal" %}
[import:1-11, lang="crystal"](code/crystal/euclidean.cr)

{% endmethod %}

Expand Down Expand Up @@ -290,6 +294,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu
[import, lang="powershell"](code/powershell/euclidean_algorithm.ps1)
{% sample lang="coco" %}
[import, lang="coconut"](code/coconut/euclidean.coco)
{% sample lang="crystal" %}
[import, lang="crystal"](code/crystal/euclidean.cr)

{% endmethod %}

Expand Down