Jump to content

Find prime n such that reversed n is also prime: Difference between revisions

Added Algol 68
m (→‎J: &. is sufficient)
(Added Algol 68)
Line 133:
383 389
34 primes.</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # find primes whose reversed digits are also prime #
INT max number = 500; # largest prime we will reverse #
INT max prime = 1 000; # enough primes to handle reversing #
# max number #
[ 1 : max prime ]BOOL p; FOR n TO UPB p DO p[ n ] := ODD n OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( UPB p ) DO
IF p[ i ] THEN
FOR s FROM i * i BY i + i TO UPB p DO p[ s ] := FALSE OD
FI
OD;
p[ 1 ] := FALSE; p[ 2 ] := TRUE;
OP FMT = ( INT n )STRING: whole( n, 0 );
INT count := 0;
FOR n TO max number DO
IF p[ n ] THEN
INT r := n MOD 10;
INT v := n;
WHILE ( v OVERAB 10 ) > 0 DO
r *:= 10 +:= v MOD 10
OD;
IF p[ r ] THEN
print( ( " ", whole( n, -3 ) ) );
IF ( count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
FI
FI
OD;
print( ( newline, "Found ", whole( count, 0 )
, " reversable primes up to ", whole( max number, 0 )
)
)
END
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157
167 179 181 191 199 311 313 337 347 353
359 373 383 389
Found 34 reversable primes up to 500</pre>
 
=={{header|ALGOL W}}==
Line 191 ⟶ 233:
Found 34 reversable primes below 500
</pre>
 
=={{header|Arturo}}==
 
3,038

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.