Skip to content

Commit ebf99c3

Browse files
authored
Add Prime Number in ALGOL 60 (#5365)
1 parent 5cf55d3 commit ebf99c3

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

archive/a/algol60/prime-number.alg

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
begin
2+
procedure usage;
3+
begin
4+
outstring(1, "Usage: please input a non-negative integer\n");
5+
stop
6+
end usage;
7+
8+
comment Input a digit character from stdin and return the following:
9+
- "0" to "9" maps to 0 to 9
10+
- "+" maps to 10
11+
- "-" maps to 11
12+
- whitespace maps to 12
13+
- comma maps to 13
14+
- null byte maps to -1
15+
- invalid bytes map to -2;
16+
integer procedure indigit;
17+
begin
18+
comment Mapping:
19+
- "0" to "9" maps to 1 to 10
20+
- "+" maps to 11
21+
- "-" maps to 12
22+
- "," mapps to 13
23+
- "\t" maps to 14
24+
- "\r" maps to 15
25+
- "\n" maps to 16
26+
- " " maps to 17
27+
- null byte maps to 18
28+
- invalid byte maps 0;
29+
integer ch;
30+
inchar(0, "0123456789+-,\t\r\n ", ch);
31+
if ch < 1 then ch := -2
32+
else if ch < 14 then ch := ch - 1
33+
else if ch < 18 then ch := 12
34+
else ch := -1;
35+
indigit := ch
36+
end indigit;
37+
38+
comment Input an integer from stdin into 'result' and parse it.
39+
The last character is read into 'ch'.
40+
return true if integer is valid, false otherwise;
41+
boolean procedure inValidInteger(result, ch);
42+
integer result, ch;
43+
begin
44+
boolean valid;
45+
integer s;
46+
47+
result := 0;
48+
valid := false;
49+
s := 1;
50+
51+
comment Ignore whitespace;
52+
for ch := indigit while ch = 12 do indigit;
53+
54+
comment Process signs: ignore "+" and invert sign if "-";
55+
signloop:
56+
if ch = 10 | ch = 11 then
57+
begin
58+
if ch = 11 then s := -s;
59+
ch := indigit;
60+
goto signloop
61+
end;
62+
63+
comment Process digits: update value;
64+
valueloop:
65+
if ch >= 0 & ch <= 9 then
66+
begin
67+
comment Invalid if overflow or underflow;
68+
valid := false;
69+
if (s > 0 & (maxint - ch) % 10 < result) |
70+
(s < 0 & (-1 - maxint + ch) % 10 > result) then goto done;
71+
72+
result := result * 10 + s * ch;
73+
ch := indigit;
74+
valid := true;
75+
goto valueloop
76+
end;
77+
78+
comment Ignore whitespace;
79+
for ch := ch while ch = 12 do ch := indigit;
80+
81+
done:
82+
inValidInteger := valid
83+
end inValidInteger;
84+
85+
integer procedure mod(x, n);
86+
value x, n;
87+
integer x, n;
88+
begin
89+
mod := x - n * (x % n)
90+
end mod;
91+
92+
boolean procedure isprime(x);
93+
value x;
94+
integer x;
95+
begin
96+
boolean result;
97+
integer i, q;
98+
99+
result := x = 2 | (x > 2 & mod(x, 2) != 0);
100+
if result then
101+
begin
102+
q := entier(sqrt(x));
103+
for i := 3 step 2 until q do
104+
begin
105+
result := mod(x, i) != 0;
106+
if !result then goto done
107+
end
108+
end;
109+
110+
done:
111+
isprime := result
112+
end isprime;
113+
114+
integer argc, result, ch;
115+
116+
comment Get number of parameters. Exit if too few;
117+
ininteger(0, argc);
118+
if argc < 1 then usage;
119+
120+
comment Get integer value from 1st argument. Exit if invalid, not
121+
end of argument, or negative;
122+
if !inValidInteger(result, ch) | ch != -1 | result < 0 then usage;
123+
124+
comment Output "prime" if number is prime, "composite" otherwise;
125+
if isprime(result) then outstring(1, "prime")
126+
else outstring(1, "composite")
127+
end

0 commit comments

Comments
 (0)