Palindromic primes: Difference between revisions

Added Oberon-07
(Added Oberon-07)
 
(6 intermediate revisions by 5 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[ n ] THEN print( ( " ", 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>
</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
END
END
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 725 ⟶ 789:
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
</pre>
'''faster'''
<syntaxhighlight lang="parigp">p10( n ) = 10^n;
rew( m, c ) = {
local( t, n );
t = 0; n = m;
for(i=1, c,
t = t*10 + n%10;
n \= 10 );
return( t ) }
range( p, w, disp = 0 ) = {
local( w10, mi, mj, z, pal, q ,k = -1);
w10 = p * p10( w ) + p;
mi = p10( w \ 2 + 1 );
mj = p10( w \ 2 );
z = p10( w \ 2 - 1 ) - 1;
for( i = 0, z,
pal = rew( i, w\2 );
q = w10 + i * mi + pal;
for( j = 0, 9,
if( isprime(q + j * mj ),
k++;
if( disp,
if((k % 8)==0,print());
print1( q + j * mj, "\t") ) ) ) );
return( [ k+1, q + 9*mj ]); }
 
gener( disp=0 ) = {
local( t=[ 1, 3, 7, 9], s=5, x,start );
start = getabstime();
for( w = 1, 8,
for( i = 1, 20 - 2*w, print1(" "));
print1( p10(w*2));
for( i = 1, 4,
print1(".");
x=range(t[i], w*2, disp);
s+=x[1]; );
printf( "\t # %8d %8.3g [sec]\n",
, s, (getabstime()-start)/1000.0) )
}</syntaxhighlight>
{{out}}
100.... # 20 0.e-19 [sec]
10000.... # 113 0.e-19 [sec]
1000000.... # 781 0.e-19 [sec]
100000000.... # 5953 0.0620 [sec]
10000000000.... # 47995 0.718 [sec]
1000000000000.... # 401696 7.72 [sec]
100000000000000.... # 3438339 86.2 [sec]
 
=={{header|Perl}}==
Line 1,033 ⟶ 1,146:
≪ { } 2
'''DO'''
'''IF''' DUP DUP <span style="color:blue">REVN</span> == '''THEN''' SWAP OVER + SWAP '''END'''
NEXTPRIME
'''UNTIL''' DUP 1000 > '''END'''
Line 1,161 ⟶ 1,274:
<pre>[2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929]
</pre>
 
=={{header|S-BASIC}}==
<syntaxhighlight lang="BASIC">
$constant FALSE = 0
$constant TRUE = 0FFFFH
 
rem - return true if n is palindromic, otherwise false
function ispalindromic(n = integer) = integer
var i, j = integer
var s = string
s = str$(n)
i = 2 rem - skip over leading sign or space
j = len(s)
while i < j and (mid(s,i,1)) = (mid(s,j,1)) do
begin
i = i + 1
j = j - 1
end
end = (mid(s,i,1)) = (mid(s,j,1))
 
rem - return n mod m
function mod(n, m = integer) = integer
end = n - m * (n / m)
 
rem - return true if n is prime, otherwise false
function isprime(n = integer) = integer
var i, limit, result = integer
if n = 2 then
result = TRUE
else if (n < 2) or (mod(n,2) = 0) then
result = FALSE
else
begin
limit = int(sqr(n))
i = 3
while (i <= limit) and (mod(n, i) <> 0) do
i = i + 2
result = not (i <= limit)
end
end = result
 
rem - main code begins here
 
var i, count = integer
print "Looking up to 1000 for palindromic primes"
count = 0
for i = 2 to 1000
if isprime(i) then
if ispalindromic(i) then
begin
print using "##### ";i;
count = count + 1
if mod(count, 6) = 0 then print
end
next i
print
print count; " were found"
 
end
</syntaxhighlight>
{{out}}
<pre>
Looking up to 1000 for palindromic primes
2 3 5 7 11 101
131 151 181 191 313 353
373 383 727 757 787 797
919 929
20 were found
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func palindromic_primes(upto, base = 10) {
Line 1,194 ⟶ 1,377:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascript">import "./mathfmt" for IntFmt
import "/fmt" for Fmt
import "/seq" for Lst
 
var reversed = Fn.new { |n|
Line 1,215 ⟶ 1,396:
System.print("Palindromic primes under 1,000:")
var smallPals = pals.where { |p| p < 1000 }.toList
Fmt.tprint("$3d", smallPals, 10)
for (chunk in Lst.chunks(smallPals, 10)) Fmt.print("$3d", chunk)
System.print("\n%(smallPals.count) such primes found.")
 
System.print("\nAdditional palindromic primes under 100,000:")
var bigPals = pals.where { |p| p >= 1000 }.toList
Fmt.tprint("$,6d", bigPals, 10)
for (chunk in Lst.chunks(bigPals, 10)) Fmt.print("$,6d", chunk)
System.print("\n%(bigPals.count) such primes found, %(pals.count) in all.")</syntaxhighlight>
 
3,021

edits