Skip to content
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
37 changes: 37 additions & 0 deletions contents/euclidean_algorithm/code/bash/euclid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 = $a - $b"
else
let "b = $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-18, 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:20-32, 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
4 changes: 1 addition & 3 deletions contents/thomas_algorithm/thomas_algorithm.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Thomas Algorithm
s# Thomas Algorithm
Copy link
Member

Choose a reason for hiding this comment

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

you typed an s here

Copy link
Member

Choose a reason for hiding this comment

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

Wait this file should not be here at all!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops got to undo commits


As alluded to in the [Gaussian Elimination chapter](../gaussian_elimination/gaussian_elimination.md), the Thomas Algorithm (or TDMA, Tri-Diagonal Matrix Algorithm) allows for programmers to **massively** cut the computational cost of their code from $$ O(n^3)$$ to $$O(n)$$ in certain cases!
This is done by exploiting a particular case of Gaussian Elimination where the matrix looks like this:
Expand Down Expand Up @@ -111,8 +111,6 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
[import, lang:"java"](code/java/thomas.java)
{% sample lang="hs" %}
[import, lang:"haskell"](code/haskell/thomas.hs)
{% sample lang="go" %}
[import, lang:"go"](code/golang/thomas.go)
Copy link
Member

Choose a reason for hiding this comment

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

You're still removing lines, you shouldn't touch thomas_algorithm.md at all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops yeah

{% sample lang="swift" %}
[import, lang:"swift"](code/swift/thomas.swift)
{% sample lang="php" %}
Expand Down