Extra primes: Difference between revisions

Added Algol 68
(added Arturo)
(Added Algol 68)
Line 184:
7253 7523 7723 7727
36 extra primes.</pre>
 
=={{header|ALGOL 68}}==
Based on the Algol W sample.
<syntaxhighlight lang="algol68">
BEGIN # find extra primes - numbers whose digits are prime and whose #
# digit sum is prime #
# the digits can only be 2, 3, 5, 7 #
# other than 1 digit numbers, the first three digits #
# can be 0, 2, 3, 5, 7 and the final digit can only be 3 and 7 #
# which means there are at most 5^3 * 2 = 250 possible numbers #
# so we will use trial division for primality testing #
# returns TRUE if n is prime, FALSE otherwise - uses trial division #
PROC is prime = ( INT n )BOOL:
IF n < 3 THEN n = 2
ELIF n MOD 3 = 0 THEN n = 3
ELIF NOT ODD n THEN FALSE
ELSE
BOOL is a prime := TRUE;
FOR f FROM 5 BY 2 WHILE f * f <= n AND is a prime DO
is a prime := n MOD f /= 0
OD;
is a prime
FI # is prime # ;
# first four numbers ) i.e.the 1 digit primes ) as a special case #
print( ( " 2 3 5 7" ) );
INT count := 4;
# 2, 3 and 5 digit numberrs #
INT d1 := 0;
TO 5 DO
INT d2 := 0;
TO 5 DO
IF ( d1 + d2 ) = 0 OR d2 /= 0 THEN
INT d3 := 2;
TO 4 DO
FOR d4 FROM 3 BY 4 TO 7 DO
INT sum = d1 + d2 + d3 + d4;
INT n = ( ( ( ( ( d1 * 10 ) + d2 ) * 10 ) + d3 ) * 10 ) + d4;
IF is prime( sum ) AND is prime( n ) THEN
# found a prime whose prime digits sum to a prime #
print( ( " ", whole( n, -4 ) ) );
IF ( count +:= 1 ) MOD 12 = 0 THEN print( ( newline ) ) FI
FI
OD;
d3 +:= 1;
IF d3 = 4 OR d3 = 6 THEN d3 +:= 1 FI
OD
FI;
d2 +:= 1;
IF d2 = 1 OR d2 = 4 OR d2 = 6 THEN d2 +:= 1 FI
OD;
d1 +:= 1;
IF d1 = 1 OR d1 = 4 OR d1 = 6 THEN d1 +:= 1 FI
OD
END
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 23 223 227 337 353 373 557 577
733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323
3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727
</pre>
 
=={{header|ALGOL W}}==
3,032

edits