diff options
| author | Abigail <abigail@abigail.be> | 2021-10-26 13:47:47 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-10-26 13:47:47 +0200 |
| commit | 079ba2d53098792f0b05de876066b1003434d8f6 (patch) | |
| tree | c6850cc141ed19d962cf1223c03b0e2965769a66 | |
| parent | 848f09825ecee5c5097a1fbc9268a77fedf34a30 (diff) | |
| download | perlweeklychallenge-club-079ba2d53098792f0b05de876066b1003434d8f6.tar.gz perlweeklychallenge-club-079ba2d53098792f0b05de876066b1003434d8f6.tar.bz2 perlweeklychallenge-club-079ba2d53098792f0b05de876066b1003434d8f6.zip | |
Use recursive algorithms for GCD
| -rw-r--r-- | challenge-136/abigail/bash/ch-1.sh | 13 | ||||
| -rw-r--r-- | challenge-136/abigail/lua/ch-1.lua | 7 | ||||
| -rw-r--r-- | challenge-136/abigail/node/ch-1.js | 7 |
3 files changed, 12 insertions, 15 deletions
diff --git a/challenge-136/abigail/bash/ch-1.sh b/challenge-136/abigail/bash/ch-1.sh index 5e7b17da18..9c3ac91402 100644 --- a/challenge-136/abigail/bash/ch-1.sh +++ b/challenge-136/abigail/bash/ch-1.sh @@ -15,13 +15,12 @@ function gcd () { local a=$1 local b=$2 - local t=0 - while ((b != 0)) - do ((t = b)) - ((b = a % b)) - ((a = t)) - done - ((gcd = a)) + if ((b > a)) + then gcd $b $a + elif ((b == 0)) + then gcd=$a + else gcd $b $((a % b)) + fi } set -f diff --git a/challenge-136/abigail/lua/ch-1.lua b/challenge-136/abigail/lua/ch-1.lua index 182d18fba7..1c3e149da6 100644 --- a/challenge-136/abigail/lua/ch-1.lua +++ b/challenge-136/abigail/lua/ch-1.lua @@ -13,10 +13,9 @@ -- (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations) -- function gcd (a, b) - while b > 0 do - a, b = b, a % b - end - return a + if b > a then return gcd (b, a) end + if b == 0 then return a end + return gcd (b, a % b) end -- diff --git a/challenge-136/abigail/node/ch-1.js b/challenge-136/abigail/node/ch-1.js index 08331ea39f..1fb04ad1c4 100644 --- a/challenge-136/abigail/node/ch-1.js +++ b/challenge-136/abigail/node/ch-1.js @@ -13,10 +13,9 @@ // (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations) // function gcd (a, b) { - while (b > 0) { - [a, b] = [b, a % b] - } - return (a) + if (b > a) {return gcd (b, a)} + if (b == 0) {return a} + return gcd (b, a % b) } // |
