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

#
# See ../README.md
#

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

set -f

BASE=10
COUNT=5

function is_long () {
    local number=$1
    local rest=0
    local seen=()

    for ((i = 0; i < number; i ++))
    do seen[$i]=0
    done

    for ((i = 2; i <= number; i ++))
    do  ((rest = (rest * BASE + BASE - 1) % number))
        if   ((${seen[$rest]} == 1))
        then is_long=0
             return
        fi
        seen[$rest]=1
    done

    is_long=1
}

number=1
while ((COUNT > 0))
do  ((number ++))
    if   ((BASE % number != 0))
    then is_long $number
         if   (($is_long == 1))
         then echo $number
              ((COUNT --))
         fi
    fi
done