Palindromic primes: Difference between revisions

Added Algol 68
m (→‎{{header|REXX}}: changed a comment.)
(Added Algol 68)
Line 5:
Find and show all palindromic primes &nbsp; <big>'''n'''</big>, &nbsp; &nbsp; where &nbsp; <big> '''n &nbsp; &lt; &nbsp; 1000''' </big>
<br><br>
 
=={{header|ALGOL 68}}==
Generates the palindrmic 3 digit numbers and uses the observations that all 1 digit primes are palindromic and that for 2 digit numbers, only multiples of 11 are palindromic and hence 11 is the only two digit palindromic prime.
<lang algol68>BEGIN # find primes that are palendromic in base 10 #
INT max prime = 999;
# sieve the primes to max prime #
[ 1 : max prime ]BOOL prime;
prime[ 1 ] := FALSE; prime[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO UPB prime DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO UPB prime DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( max prime ) DO
IF prime[ i ] THEN FOR s FROM i * i BY i + i TO UPB prime DO prime[ s ] := FALSE OD FI
OD;
# print the palendromic primes in the base 10 #
# all 1 digit primes are palindromic #
FOR n TO 9 DO IF prime[ n ] THEN print( ( " ", whole( n, 0 ) ) ) FI OD;
# the only palindromic 2 digit numbers are multiples of 11 #
# so 11 is the only 2 digit palindromic prime #
print( ( " 11" ) );
# three digit numbers, the first and last digits must be odd #
# and cannot be 5 (as the number would be divisible by 5) #
FOR fl BY 2 TO 9 DO
IF fl /= 5 THEN
FOR m FROM 0 TO 9 DO
INT n = ( ( ( fl * 10 ) + m ) * 10 ) + fl;
IF prime[ n ] THEN
# have a palindromic prime #
print( ( " ", whole( n, 0 ) ) )
FI
OD
FI
OD;
print( ( newline ) )
END</lang>
{{out}}
<pre>
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
</pre>
 
=={{header|Arturo}}==
3,028

edits