From b4715406dd11296d604b36ab31b238ad17cd4fe9 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 25 Oct 2021 16:50:26 +0200 Subject: Bash solution for week 136, part 1 --- challenge-136/abigail/README.md | 1 + challenge-136/abigail/bash/ch-1.sh | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenge-136/abigail/bash/ch-1.sh 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 -- cgit