aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-10-25 16:50:26 +0200
committerAbigail <abigail@abigail.be>2021-10-25 16:50:26 +0200
commitb4715406dd11296d604b36ab31b238ad17cd4fe9 (patch)
tree5c6507ae85cef9723563692d1b7202cc0786e2fe
parent593e3b55cf0395b75891730d28f8a9eb7a26951a (diff)
downloadperlweeklychallenge-club-b4715406dd11296d604b36ab31b238ad17cd4fe9.tar.gz
perlweeklychallenge-club-b4715406dd11296d604b36ab31b238ad17cd4fe9.tar.bz2
perlweeklychallenge-club-b4715406dd11296d604b36ab31b238ad17cd4fe9.zip
Bash solution for week 136, part 1
-rw-r--r--challenge-136/abigail/README.md1
-rw-r--r--challenge-136/abigail/bash/ch-1.sh41
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-136/abigail/README.md b/challenge-136/abigail/README.md
index ef4c25705c..3920207947 100644
--- a/challenge-136/abigail/README.md
+++ b/challenge-136/abigail/README.md
@@ -3,6 +3,7 @@
## Part 1
* [GNU AWK](awk/ch-1.gawk)
+* [Bash](bash/ch-1.sh)
* [Perl](perl/ch-1.pl)
## Part 2
diff --git a/challenge-136/abigail/bash/ch-1.sh b/challenge-136/abigail/bash/ch-1.sh
new file mode 100644
index 0000000000..5e7b17da18
--- /dev/null
+++ b/challenge-136/abigail/bash/ch-1.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-1.sh < input-file
+#
+
+#
+# Find the GCD, using Euclids algorithm
+# (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations)
+#
+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))
+}
+
+set -f
+
+#
+# Calculate the powers of 2 (greater than 1). 2^62 is the max a bash
+# integer holds
+#
+declare -a power_of_2
+for ((i = 1; i <= 62; i ++))
+do power_of_2[$((1 << i))]=1
+done
+
+while read n m
+do gcd $n $m
+ echo ${power_of_2[$gcd]:-0}
+done