diff options
Diffstat (limited to 'challenge-106/abigail/bash/ch-2.sh')
| -rw-r--r-- | challenge-106/abigail/bash/ch-2.sh | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-106/abigail/bash/ch-2.sh b/challenge-106/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..77f38b81c3 --- /dev/null +++ b/challenge-106/abigail/bash/ch-2.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare fraction + +function long_division () { + declare numerator=$1 + declare denominator=$2 + declare BASE=10 + fraction=$((numerator / denominator)). + declare position=${#fraction} + declare -a seen + + ((numerator %= denominator)) + + while ((!seen[numerator])) + do if ((numerator == 0)) + then return + fi + seen[$numerator]=$position + fraction=$fraction$((BASE * numerator / denominator)) + ((numerator = BASE * numerator % denominator)) + ((position ++)) + done + fraction=${fraction::${seen[$numerator]}}\(${fraction:${seen[$numerator]}}\) +} + + +while read numerator denominator +do long_division $numerator $denominator + echo $fraction +done + + |
