blob: 24dc5146522ad2f4f70900803d74b965c47c84fb (
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
Program XXX;
(* *)
(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-147 *)
(* *)
(* *)
(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out *)
(* *)
var
todo: array of integer;
new_todo: array of integer;
pow, i, d: integer;
count: integer = 20;
candidate: integer;
function is_prime (p: integer): boolean;
var
i: integer;
begin
if p = 2 then begin
is_prime := true;
exit;
end;
if p mod 2 = 0 then begin
is_prime := false;
exit;
end;
i := 3;
while i * i <= p do begin
if p mod i = 0 then begin
is_prime := false;
exit;
end;
i := i + 2;
end;
is_prime := true;
exit;
end;
begin
setlength (todo, 4);
todo [0] := 2;
todo [1] := 3;
todo [2] := 5;
todo [3] := 7;
for i := 0 to 3 do begin
write (todo [i], ' ');
end;
count := count - 4;
pow := 10;
while count > 0 do begin
setlength (new_todo, 0);
for d := 1 to 9 do begin
for i := 0 to length (todo) - 1 do begin
candidate := d * pow + todo [i];
if is_prime (candidate) then begin
insert (candidate, new_todo, length (new_todo));
write (candidate, ' ');
count := count - 1;
if count <= 0 then begin
break;
end;
end;
if count <= 0 then begin
break;
end;
end;
if count <= 0 then begin
break;
end;
end;
todo := new_todo;
pow := pow * 10;
end;
writeln ('');
end.
end.
|