blob: c39dc35247226cf29bd36b919c7a01b403e5b3c1 (
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
|
#!/usr/bin/awk
#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
#
#
# Run as: awk -f ch-1.awk
#
func is_prime (n, i) {
if (n % 2 == 0) {return 0}
for (i = 3; i <= sqrt (n); i += 2) {
if (n % i == 0) {
return 0
}
}
return 1
}
BEGIN {
todo [1] = 2
todo [2] = 3
todo [3] = 5
todo [4] = 7
n = 4
i = 1
count = 20 - n;
printf ("2 3 5 7 ")
low = 1
high = n
while (count > 0) {
for (d = 1; d <= 9 && count > 0; d ++) {
for (i = low; i <= high && count > 0; i ++) {
candidate = d todo [i]
if (is_prime(candidate) == 1) {
printf candidate " "
count --;
todo [++ n] = candidate;
}
}
}
low = high + 1
high = n
}
printf ("\n")
}
|