aboutsummaryrefslogtreecommitdiff
path: root/challenge-108/abigail/bash/ch-2.sh
blob: c74aff4bbe5eabdcca25d07b2af3591a34c9920b (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
#

set -f

COUNT=10

function index () {
    local x=$1
    local y=$2
    idx=$((COUNT * x + y))
}


if   [ "X$1" != "Xcompute" ]
then echo "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147"
else bell[0]=1
     for ((x = 1; x < COUNT - 1; x ++))
     do   index $x 0;                  i1=$idx
          index $((x - 1)) $((x - 1)); i2=$idx
          bell[$i1]=${bell[$i2]}
          for ((y = 1; y <= x; y ++))
          do  index   $x         $y;       i1=$idx
              index   $x       $((y - 1)); i2=$idx
              index $((x - 1)) $((y - 1)); i3=$idx
              bell[$i1]=$((bell[i2] + bell[i3]))
          done
     done

     printf "1"
     for ((x = 0; x < COUNT - 1; x ++))
     do index $x $x;
        printf ", %d" ${bell[$idx]}
     done
     echo
fi