aboutsummaryrefslogtreecommitdiff
path: root/challenge-147/abigail/bash/ch-1.sh
blob: 1f5f09be50c9360fb5cfc62cbbf8f04b19ccbd1c (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
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/sh

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

#
# Run as: bash ch-1.sh
#

set -f

function is_prime () {
    local n=$1
    local i
    is_prime=1
    if ((n > 2 && n % 2 == 0)) 
    then is_prime=0
    else for ((i = 3; i * i <= n && is_prime == 1; i += 2))
         do  if ((n % i == 0))
             then is_prime=0
             fi
         done
    fi
}

todo=(2 3 5 7)
count=20

for p in "${todo[@]}"
do  printf "$p "
    ((count --))
done


while ((count > 0))
do    next=()
      for ((d = 1; d <= 9 && count > 0; d ++))
      do  for p in "${todo[@]}"
          do  candidate=$d$p
              is_prime $candidate
              if (($is_prime == 1))
              then printf $candidate" "
                   ((count --))
                   next+=($candidate)
                   if ((count <= 0))
                   then break
                   fi
              fi
          done
     done
     todo=("${next[@]}")
done

echo