aboutsummaryrefslogtreecommitdiff
path: root/challenge-147/abigail/r/ch-1.r
blob: ef6dddc6782b03b70b6c9e466a1f2ee397c97bcd (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
#!/usr/local/bin/Rscript

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

#
# Run as: Rscript ch-1.r
#

suppressPackageStartupMessages (
    library (gmp)
)

todo = c (2, 3, 5, 7)
cat (todo, "")

count = 20 - length (todo)

pow = 10
while (count > 0 && length (todo) > 0) {
    new_todo = c ()
    for (d in 1 : 9) {
        for (p in todo) {
            candidate = d * pow + p
            if (isprime (candidate) > 0) {
                cat (candidate, "")
                count = count - 1
                new_todo = c (new_todo, candidate)
                if (count <= 0) {
                    break
                }
            }
            if (count <= 0) {
                break
            }
        }
        if (count <= 0) {
            break
        }
    }
    pow = pow * 10
    todo = new_todo
}

cat ("\n")