aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/abigail/bash/ch-1.sh
blob: b2f98abec96ce04acf9f10a5a7be46f6e07a3d98 (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
#!/bin/sh

#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
#

#
# Run as: bash ch-1.sh < input-file
#

set -f

declare -a ugly
ugly[0]=1
((next_2 = 0))
((next_3 = 0))
((next_5 = 0))
count=1

while read n
do  while ((count < n))
    do  ((c2 = 2 * ${ugly[next_2]}))
        ((c3 = 3 * ${ugly[next_3]}))
        ((c5 = 5 * ${ugly[next_5]}))

        if ((c2 <= c3 && c2 <= c5)); then ((ugly[count] = c2)); fi
        if ((c3 <= c2 && c3 <= c5)); then ((ugly[count] = c3)); fi
        if ((c5 <= c3 && c5 <= c2)); then ((ugly[count] = c5)); fi

        if ((2 * ${ugly[next_2]} <= ${ugly[count]})); then ((next_2 ++)); fi
        if ((3 * ${ugly[next_3]} <= ${ugly[count]})); then ((next_3 ++)); fi
        if ((5 * ${ugly[next_5]} <= ${ugly[count]})); then ((next_5 ++)); fi

        ((count ++))
    done
    echo ${ugly[$((n - 1))]}
done