Prime words: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 23:
 
<br><br>
=={{header|ALGOL 68}}==
Does not distinguish between letters and non-letter ASCII codes (as with the REXX sample).
<lang algol68># find words whose character codes are primes #
IF FILE input file;
STRING file name = "unixdict.txt";
open( input file, file name, stand in channel ) /= 0
THEN
# failed to open the file #
print( ( "Unable to open """ + file name + """", newline ) )
ELSE
# file opened OK #
BOOL at eof := FALSE;
# set the EOF handler for the file #
on logical file end( input file, ( REF FILE f )BOOL:
BEGIN
# note that we reached EOF on the #
# latest read #
at eof := TRUE;
# return TRUE so processing can continue #
TRUE
END
);
# construct a sieve of primes up to the maximum character #
[ 0 : max abs char ]BOOL s;
FOR i TO UPB s DO s[ i ] := TRUE OD;
s[ 0 ] := s[ 1 ] := FALSE;
FOR i FROM 2 TO ENTIER sqrt( UPB s ) DO
IF s[ i ] THEN FOR p FROM i * i BY i TO UPB s DO s[ p ] := FALSE OD FI
OD;
# find the prime words #
INT prime count := 0;
WHILE STRING word;
get( input file, ( word, newline ) );
NOT at eof
DO
BOOL is prime := FALSE;
FOR w pos FROM LWB word TO UPB word WHILE ( is prime := s[ ABS word[ w pos ] ] ) DO SKIP OD;
IF is prime THEN
prime count +:= 1;
print( ( whole( prime count, -5 ), ": ", word, newline ) )
FI
OD;
close( input file )
FI</lang>
{{out}}
<pre>
1: a
2: aaa
3: age
4: agee
5: ak
6: am
7: ama
8: e
9: egg
10: eke
11: em
12: emma
13: g
14: ga
15: gag
16: gage
17: gam
18: game
19: gamma
20: ge
21: gee
22: gem
23: gemma
24: gm
25: k
26: keg
27: m
28: ma
29: mae
30: magma
31: make
32: mamma
33: me
34: meek
35: meg
36: q
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
3,032

edits