Palindromic primes: Difference between revisions

Added Oberon-07
(Added S-BASIC example)
(Added Oberon-07)
(2 intermediate revisions by 2 users not shown)
Line 80:
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.
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find primes that are palendromic in base 10 #
INT max prime = 999;
# sieve the primes to max prime #
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE max prime;
# print the palendromic primes in the base 10 #
# all 1 digit primes are palindromic #
FOR# nthe TOonly 9palindromic DO2 IFdigit prime[numbers nare ]multiples THENof print(11 ( " ", whole( n, 0 ) ) ) FI OD; #
# so 11 is the only palindromicpossible 2 digit numberspalindromic prime are multiples of 11 #
FOR n TO 11 DO IF prime[ 11n ] THEN print( ( " 11", whole( n, 0 ) ) ) FI OD;
# so 11 is the only possible 2 digit palindromic prime #
# three digit numbers, the first and last digits must be odd #
IF prime[ 11 ] THEN print( ( " 11" ) ) FI;
# threeand digitcannot numbers,be 5 (as the firstnumber would be divisible by 5) 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
Line 105 ⟶ 104:
OD;
print( ( newline ) )
END
END</syntaxhighlight>
{{out}}
<pre>
Line 715:
<pre> 2 3 5 7 11 101 131 151 181 191
313 353 373 383 727 757 787 797 919 929</pre>
 
=={{header|Oberon-07}}==
Based on the Algol 68 sample with the Sieve routine from the Additive Primes task.
<syntaxhighlight lang="modula2">
MODULE PalindromicPrimes; (* find primes that are palendromic in base 10 *)
IMPORT
Out;
 
CONST
Max = 999;
 
VAR
fl, m, n :INTEGER;
Prime :ARRAY Max + 1 OF BOOLEAN;
 
PROCEDURE Sieve;
VAR i, j :INTEGER;
BEGIN
Prime[ 0 ] := FALSE; Prime[ 1 ] := FALSE;
FOR i := 2 TO Max DO Prime[ i ] := TRUE END;
FOR i := 2 TO Max DIV 2 DO
IF Prime[ i ] THEN
j := i * 2;
WHILE j <= Max DO
Prime[ j ] := FALSE;
j := j + i
result = TRUEEND
END
else END
END Sieve;
 
PROCEDURE OutN;
BEGIN
Out.String( " " );Out.Int( n, 0 )
END OutN;
 
BEGIN
Sieve;
(* print the palendromic primes in the base 10 *)
(* all 1 digit primes are palindromic *)
(* the only palindromic 2 digit numbers are multiples of 11 *)
#(* so 11 is the only possible 2 digit palindromic prime # *)
FOR n := 1 TO 11 DO IF Prime[ n ] THEN OutN END END;
 
(* 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 := 1 TO 9 BY 2 DO
IF fl # 5 THEN
FOR m := 0 TO 9 DO
n := ( ( ( fl * 10 ) + m ) * 10 ) + fl;
IF Prime[ n ] THEN
(* have a palindromic prime *)
OutN
END
END
END
END;
Out.Ln
END PalindromicPrimes.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
</pre>
 
=={{header|PARI/GP}}==
Line 1,217 ⟶ 1,281:
 
rem - return true if n is palindromic, otherwise false
function ispalindromeispalindromic(n = integer) = integer
var i, j, result = integer
var s = string
s = str$(n)
Line 1,228 ⟶ 1,292:
j = j - 1
end
end if= (mid(s,i,1)) = (mid(s,j,1)) then
result = TRUE
else
result = FALSE
end = result
 
rem - return n mod m
Line 1,262 ⟶ 1,322:
for i = 2 to 1000
if isprime(i) then
if ispalindromeispalindromic(i) then
begin
print using "##### ";i;
3,043

edits