Euclidean Algorithm in Bash
· One min read
-
Let C be the remainder when A is divided by B.
-
Let D be the remainder when B is divided by C.
-
Let E be the remainder when C is divided by D.
...
-
Let Y be the remainder when X is divided by 0.
Then Y is the greatest common divisor.
Bash Script
#!/usr/bin/env bash
function gcd(){
test $2 -eq 0 && echo $1 || gcd $2 $(( $1 % $2 ))
}
gcd 1071 1029
# 21
