Palindromic primes: Difference between revisions

Added Oberon-07
(Added 11l)
(Added Oberon-07)
(17 intermediate revisions by 12 users not shown)
Line 7:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 22:
I s == reversed(s)
print(n, end' ‘ ’)
print()</langsyntaxhighlight>
 
{{out}}
Line 31:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsPalindromicPrime(INT i BYTE ARRAY primes)
Line 68:
OD
PrintF("%E%EThere are %I palindromic primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindromic_primes.png Screenshot from Atari 8-bit computer]
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}}
<langsyntaxhighlight 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</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 113:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">loop split.every: 10 select 2..1000 'x [
and? prime? x
x = to :integer reverse to :string x
] 'a -> print map a => [pad to :string & 4]</langsyntaxhighlight>
 
{{out}}
Line 124:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PALINDROMIC_PRIMES.AWK
BEGIN {
Line 155:
return(rts)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 161:
Palindromic primes 1-999: 20
</pre>
 
=={{header|C++}}==
This includes a solution for the similar task [[Palindromic primes in base 16]].
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
 
unsigned int reverse(unsigned int base, unsigned int n) {
unsigned int rev = 0;
for (; n > 0; n /= base)
rev = rev * base + (n % base);
return rev;
}
 
class palindrome_generator {
public:
explicit palindrome_generator(unsigned int base)
: base_(base), upper_(base) {}
unsigned int next_palindrome();
 
private:
unsigned int base_;
unsigned int lower_ = 1;
unsigned int upper_;
unsigned int next_ = 0;
bool even_ = false;
};
 
unsigned int palindrome_generator::next_palindrome() {
++next_;
if (next_ == upper_) {
if (even_) {
lower_ = upper_;
upper_ *= base_;
}
next_ = lower_;
even_ = !even_;
}
return even_ ? next_ * upper_ + reverse(base_, next_)
: next_ * lower_ + reverse(base_, next_ / base_);
}
 
bool is_prime(unsigned int n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
for (unsigned int p = 5; p * p <= n; p += 4) {
if (n % p == 0)
return false;
p += 2;
if (n % p == 0)
return false;
}
return true;
}
 
std::string to_string(unsigned int base, unsigned int n) {
assert(base <= 36);
static constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string str;
for (; n != 0; n /= base)
str += digits[n % base];
std::reverse(str.begin(), str.end());
return str;
}
 
void print_palindromic_primes(unsigned int base, unsigned int limit) {
auto width =
static_cast<unsigned int>(std::ceil(std::log(limit) / std::log(base)));
unsigned int count = 0;
auto columns = 80 / (width + 1);
std::cout << "Base " << base << " palindromic primes less than " << limit
<< ":\n";
palindrome_generator pgen(base);
unsigned int palindrome;
while ((palindrome = pgen.next_palindrome()) < limit) {
if (is_prime(palindrome)) {
++count;
std::cout << std::setw(width) << to_string(base, palindrome)
<< (count % columns == 0 ? '\n' : ' ');
}
}
if (count % columns != 0)
std::cout << '\n';
std::cout << "Count: " << count << '\n';
}
 
void count_palindromic_primes(unsigned int base, unsigned int limit) {
unsigned int count = 0;
palindrome_generator pgen(base);
unsigned int palindrome;
while ((palindrome = pgen.next_palindrome()) < limit)
if (is_prime(palindrome))
++count;
std::cout << "Number of base " << base << " palindromic primes less than "
<< limit << ": " << count << '\n';
}
 
int main() {
print_palindromic_primes(10, 1000);
std::cout << '\n';
print_palindromic_primes(10, 100000);
std::cout << '\n';
count_palindromic_primes(10, 1000000000);
std::cout << '\n';
print_palindromic_primes(16, 500);
}</syntaxhighlight>
 
{{out}}
<pre>
Base 10 palindromic primes less than 1000:
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
Count: 20
 
Base 10 palindromic primes less than 100000:
2 3 5 7 11 101 131 151 181 191 313 353 373
383 727 757 787 797 919 929 10301 10501 10601 11311 11411 12421
12721 12821 13331 13831 13931 14341 14741 15451 15551 16061 16361 16561 16661
17471 17971 18181 18481 19391 19891 19991 30103 30203 30403 30703 30803 31013
31513 32323 32423 33533 34543 34843 35053 35153 35353 35753 36263 36563 37273
37573 38083 38183 38783 39293 70207 70507 70607 71317 71917 72227 72727 73037
73237 73637 74047 74747 75557 76367 76667 77377 77477 77977 78487 78787 78887
79397 79697 79997 90709 91019 93139 93239 93739 94049 94349 94649 94849 94949
95959 96269 96469 96769 97379 97579 97879 98389 98689
Count: 113
 
Number of base 10 palindromic primes less than 1000000000: 5953
 
Base 16 palindromic primes less than 500:
2 3 5 7 B D 11 101 151 161 191 1B1 1C1
Count: 13
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
function IsPalindrome(N, Base: integer): boolean;
{Test if number is the same forward or backward}
{For a specific Radix}
var S1,S2: string;
begin
S1:=GetRadixString(N,Base);
S2:=ReverseString(S1);
Result:=S1=S2;
end;
 
 
procedure ShowPalindromePrimes(Memo: TMemo);
var I: integer;
var Cnt: integer;
var S: string;
begin
Cnt:=0;
for I:=1 to 1000-1 do
if IsPrime(I) then
if IsPalindrome(I,10) then
begin
Inc(Cnt);
S:=S+Format('%4D',[I]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count='+IntToStr(Cnt));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11
101 131 151 181 191
313 353 373 383 727
757 787 797 919 929
 
Count=20
Elapsed Time: 2.117 ms.
</pre>
 
 
=={{header|Factor}}==
Line 166 ⟶ 374:
A simple solution that suffices for the task:
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: kernel math.primes present prettyprint sequences ;
 
1000 primes-upto [ present dup reverse = ] filter stack.</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 195 ⟶ 403:
A much more efficient solution that generates palindromic numbers directly and filters primes from them:
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: io kernel lists lists.lazy math math.functions
math.primes math.ranges prettyprint sequences
tools.memory.private ;
Line 220 ⟶ 428:
 
"Palindromic primes less than 1,000:" print
lpalindrome-primes [ 1000 < ] lwhile [ . ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 250 ⟶ 458:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function is_pal( s as string ) as boolean
Line 262 ⟶ 470:
for i as uinteger = 2 to 999
if is_pal( str(i) ) andalso isprime(i) then print i;" ";
next i : print</langsyntaxhighlight>
{{out}}<pre>
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929</pre>
Line 269 ⟶ 477:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 310 ⟶ 518:
fmt.Println()
fmt.Println(len(bigPals), "such primes found,", len(pals), "in all.")
}</langsyntaxhighlight>
 
{{out}}
Line 318 ⟶ 526:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
 
palindromicPrimes :: [Integer]
Line 329 ⟶ 537:
takeWhile
(1000 >)
palindromicPrimes</langsyntaxhighlight>
{{Out}}
<pre>2
Line 351 ⟶ 559:
919
929</pre>
 
=={{header|J}}==
 
<syntaxhighlight lang=J> palindromic=: (-: |.)@":@>
(#~ palindromic) p: i. p:inv 1000
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929</syntaxhighlight>
 
=={{header|jq}}==
Line 363 ⟶ 577:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def count(s): reduce s as $x (null; .+1);
 
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</langsyntaxhighlight>
'''Naive version'''
<syntaxhighlight lang="jq">
<lang jq>
def primes:
2, (range(3;infinite;2) | select(is_prime));
Line 373 ⟶ 587:
def palindromic_primes_slowly:
primes | select( tostring|explode | (. == reverse));
</syntaxhighlight>
</lang>
'''Less naive version'''
<langsyntaxhighlight lang="jq"># Output: an unbounded stream of palindromic primes
def palindromic_primes:
 
Line 406 ⟶ 620:
2, 3, 5, 7, 11,
(palindromic_candidates | tonumber | select(is_prime));</langsyntaxhighlight>
'''Demonstrations'''
<langsyntaxhighlight lang="jq">"Palindromic primes < 1000:",
emit_until(. >= 1000; palindromic_primes),
 
((range(5;11) | pow(10;.)) as $n
| "\nNumber of palindromic primes <= \($n): \(count(emit_until(. >= $n; palindromic_primes)))" )</langsyntaxhighlight>
{{out}}
<pre>
Line 452 ⟶ 666:
=={{header|Julia}}==
Generator method.
<langsyntaxhighlight lang="julia">using Primes
 
parray = [2, 3, 5, 7, 9, 11]
Line 459 ⟶ 673:
 
println(results)
</langsyntaxhighlight>{{out}}
<pre>[2, 3, 5, 7, 9, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Select[Range[999], PrimeQ[#] \[And] PalindromeQ[#] &]</langsyntaxhighlight>
{{out}}
<pre>{2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
const N = 999
Line 496 ⟶ 710:
for i, n in result:
stdout.write ($n).align(3)
stdout.write if (i + 1) mod 10 == 0: '\n' else: ' '</langsyntaxhighlight>
 
{{out}}
<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}}==
'''naive'''
<syntaxhighlight lang="parigp">forprime(i = 2, 1000,
if( i == fromdigits( Vecrev( digits( i ) )) ,
print1( i, " " ) ) );</syntaxhighlight>
{{out}}
<pre>
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}}==
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Palindromic_primes
use warnings;
 
$_ == reverse and (1 x $_ ) !~ /^(11+)\1+$/ and print "$_\n " for 2 .. 1e3;</langsyntaxhighlight>
{{out}}
<pre>2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929</pre>
2
3
5
7
11
101
131
151
181
191
313
353
373
383
727
757
787
797
919
929
 
=={{header|Phix}}==
===filter primes for palindromicness===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">palindrome</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
Line 543 ⟶ 861:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"found %d &lt; %,d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 550 ⟶ 868:
</pre>
===filter palindromes for primality===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
Line 564 ⟶ 882:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"found %d &lt; %,d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
Same output. Didn't actually test if this way was any faster, but expect it would be.
 
=={{header|Python}}==
A non-finite generator of palindromic primes – one of many approaches to solving this problem in Python.
<langsyntaxhighlight lang="python">'''Palindromic primes'''
 
from itertools import takewhile
Line 616 ⟶ 934:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
{{Out}}
<pre>2
Line 643 ⟶ 961:
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]]
 
<langsyntaxhighlight Quackerylang="quackery"> [ [] swap
[ base share /mod
rot swap join swap
Line 656 ⟶ 974:
[ i^ isprime if
[ i^ digits palindromic if
[ i^ echo sp ] ] ]</langsyntaxhighlight>
 
{{out}}
Line 664 ⟶ 982:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say "{+$_} matching numbers:\n{.batch(10)».fmt('%3d').join: "\n"}"
given (^1000).grep: { .is-prime and $_ eq .flip };</langsyntaxhighlight>
{{out}}
<pre>20 matching numbers:
Line 672 ⟶ 990:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays palindromic primes in base ten for all N < 1,000.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 713 ⟶ 1,031:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 747 ⟶ 1,065:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
Line 778 ⟶ 1,096:
ok
next
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 815 ⟶ 1,133:
Found 113 palindromic primes that are < 100,000
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49g}}
≪ →STR → n
≪ ""
n SIZE 1 '''FOR''' j
n j DUP SUB +
-1 '''STEP''' STR→
≫ ≫ '<span style="color:blue">REVN</span>' STO
≪ { } 2
'''DO'''
'''IF''' DUP DUP <span style="color:blue">REVN</span> == '''THEN''' SWAP OVER + SWAP '''END'''
NEXTPRIME
'''UNTIL''' DUP 1000 > '''END'''
DROP
≫ '<span style="color:blue">TASK</span>' STO
 
{{out}
<pre>
1: {2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929}
</pre>
 
=={{header|Rust}}==
This includes a solution for the similar task [[Palindromic primes in base 16]].
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// radix_fmt = "1.0"
Line 900 ⟶ 1,240:
println!();
print_palindromic_primes(16, 500);
}</langsyntaxhighlight>
 
{{out}}
Line 925 ⟶ 1,265:
2 3 5 7 b d 11 101 151 161 191 1b1 1c1
Count: 13
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
p Prime.each(1000).select{|pr| pr.digits == pr.digits.reverse}</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|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}}==
<langsyntaxhighlight lang="ruby">func palindromic_primes(upto, base = 10) {
var list = []
for (var p = 2; p <= upto; p = p.next_palindrome(base)) {
Line 941 ⟶ 1,358:
var count = palindromic_primes(10**n).len
say "There are #{count} palindromic primes <= 10^#{n}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 960 ⟶ 1,377:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-seq}}
<lang ecmascript>import "./mathfmt" for IntFmt
import "/fmt" for Fmt
import "/seq" for Lst
 
var reversed = Fn.new { |n|
Line 981 ⟶ 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.")</langsyntaxhighlight>
 
{{out}}
Line 1,013 ⟶ 1,428:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,038 ⟶ 1,453:
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
]</langsyntaxhighlight>
 
{{out}}
3,026

edits