Skip to content

Euclid algorithm in bash #497

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

Merged
merged 14 commits into from
Oct 13, 2018
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@
{
"lang": "lolcode",
"name": "LOLCODE"
},
{
"lang": "sh",
"name": "Bash"
}
]
}
Expand Down
43 changes: 43 additions & 0 deletions contents/euclidean_algorithm/code/bash/euclid.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
abs() {
local ret=$1
if (( ret < 0 )); then
((ret *= -1))
fi
printf "%s" "$ret"
}

euclid_mod() {
local a
local b
a=$(abs "$1")
b=$(abs "$2")

while (( b != 0 )); do
((tmp = b))
((b = a % b))
((a = tmp))
done
printf "%s" "$a"
}

euclid_sub() {
local a
local b
a=$(abs "$1")
b=$(abs "$2")

while (( a != b )); do
if (( a > b )); then
((a -= b))
else
((b -= a))
fi
done
printf "%s" "$a"
}

result=$(euclid_mod $((64 * 67)) $((64 * 81)))
echo "$result"
result=$(euclid_sub $((128 * 12)) $((128 * 77)))
echo "$result"
7 changes: 7 additions & 0 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
[import:2-17, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic)
{% sample lang="lolcode" %}
[import:25-40, lang="LOLCODE"](code/lolcode/euclid.lol)
{% sample lang="bash" %}
[import:24-38, lang="bash"](code/bash/euclid.bash)
{% endmethod %}

Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this:
Expand Down Expand Up @@ -124,6 +126,8 @@ Modern implementations, though, often use the modulus operator (%) like so
[import:19-31, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic)
{% sample lang="lolcode" %}
[import:9-23, lang="LOLCODE"](code/lolcode/euclid.lol)
{% sample lang="bash" %}
[import:10-22, lang="bash"](code/bash/euclid.bash)
{% endmethod %}

Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps:
Expand Down Expand Up @@ -197,8 +201,11 @@ and modulo method:
[import, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic)
{% sample lang="lolcode" %}
[import, lang="LOLCODE"](code/lolcode/euclid.lol)
{% sample lang="bash" %}
[import, lang="bash"](code/bash/euclid.bash)
{% endmethod %}

<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>