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": "bash",
Copy link
Member

Choose a reason for hiding this comment

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

We usually make lang the file extension so sh in this case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah ok

"name": "Bash"
}
]
}
Expand Down
38 changes: 38 additions & 0 deletions contents/euclidean_algorithm/code/bash/euclid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
abs() {
local ret=$1
if [ $ret -lt 0 ]; then
Copy link
Member

Choose a reason for hiding this comment

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

[ ... ] is sh (the original Bourne shell). Please use [[ ... ]] for consistency.

Copy link
Member

Choose a reason for hiding this comment

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

Though, you can do even better considering the ((...)) syntax (left as an exercise for the reader.

let "ret = $ret * -1"
Copy link
Member

Choose a reason for hiding this comment

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

While we are working with integers, do general arithmetic instead, using more modern Bash:

((ret *= -1))

fi
echo $ret
}

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

while [ $b -ne 0 ]; do
let "tmp = $b"
let "b = $a % $b"
let "a = tmp"
done
echo $a
Copy link
Member

Choose a reason for hiding this comment

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

Prefer printf for function returns. The echos at the end of the script are fine.

}

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

while [ $a -ne $b ]; do
if [ $a -gt $b ]; then
let "a -= $b"
else
let "b -= $a"
fi
done
echo $a
}

result=$(euclid_mod 143 693)
Copy link
Member

Choose a reason for hiding this comment

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

Use the other examples for consistency:

result=$(euclid_mod $((64 * 67)) $((64 * 81)))

etc.

echo $result
result=$(euclid_sub 150 400)
echo $result
6 changes: 6 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:9-19, lang="bash"](code/bash/euclid.sh)
{% 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:21-33, lang="bash"](code/bash/euclid.sh)
{% 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,6 +201,8 @@ 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.sh)
{% endmethod %}

<script>
Expand Down