blob: 77f38b81c3dc69d4cf44100571af8eeca10a5004 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
|