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

m
(Add CLU)
 
(29 intermediate revisions by 16 users not shown)
Line 3:
;Task
Find prime &nbsp; '''n''' &nbsp; &nbsp; for &nbsp; '''0 < n < 500''' &nbsp; &nbsp; which are also primes when the (decimal) digits are reversed.
<br>Nearly [https://rosettacode.org/wiki/Emirp_primes Emirp Primes], which don't count palindromatic primes like 101.
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
 
L(n) 1..499
I is_prime(n) & is_prime(Int(reversed(String(n))))
print(n, 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
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC IsPrimeAndItsReverse(INT i BYTE ARRAY primes)
Line 41 ⟶ 61:
OD
PrintF("%E%EThere are %I primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_prime_n_such_that_reversed_n_is_also_prime.png Screenshot from Atari 8-bit computer]
Line 51 ⟶ 71:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Reverse_Prime is
Line 105 ⟶ 125:
New_Line;
Put_Line (Count'Image & " primes.");
end Reverse_Prime;</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 31
Line 113 ⟶ 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}}==
<langsyntaxhighlight lang="algolw">begin % find some primes whose digits reversed is also prime %
% sets p( 1 :: n ) to a sieve of primes up to n %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
Line 164 ⟶ 226:
write( i_w := 1, s_w := 0, "Found ", pCount, " reversable primes below ", MAX_NUMBER )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 171 ⟶ 233:
Found 34 reversable primes below 500
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print
select 1..499 'x ->
and? [prime? x][prime? to :integer reverse to :string x]</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</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_PRIME_N_FOR_THAT_REVERSED_N_IS_ALSO_PRIME.AWK
BEGIN {
Line 202 ⟶ 275:
return( substr(str,start,1) revstr(str,start-1) )
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 211 ⟶ 284:
Reversible primes 1-500: 34
</pre>
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 DEFINT A-Z: MP=999: MX=500
15 MP=10^FIX(LOG(MX)/LOG(10)+1)
20 DIM C(MP): C(0)=-1: C(1)=-1
30 FOR P=2 TO SQR(MP)
40 FOR C=P+P TO MP STEP P: C(C)=-1: NEXT
50 NEXT
60 FOR N=1 TO MX: IF C(N) THEN 100
70 R=0: V=N
80 IF V>0 THEN R=10*R+V MOD 10: V=V\10: GOTO 80
90 IF NOT C(R) THEN PRINT N,
100 NEXT</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</pre>
 
=={{header|bc}}==
<syntaxhighlight lang="bc">define t(n) {
auto i, p
if (n == 2) return(1)
for (i = 0; n % (p = a[i]) != 0; ++i) if (p * p > n) return(1)
return(0)
}
 
define r(n) {
auto m
for (m = 0; n > 9; n /= 10) m = (n % 10 + m) * 10
return(m + n)
}
 
for (n = 2; n != 500; ++n) if (t(n) != 0) a[o++] = n
 
for (i = 0; i != o; ++i) if (t(r(n = a[i])) != 0) n</syntaxhighlight>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let sieve(prime, n) be
$( 0!prime := false
1!prime := false
for i = 2 to n do i!prime := true
for p = 2 to n/2 if p!prime
$( let c = p*2
while c <= n
$( c!prime := false
c := c+p
$)
$)
$)
 
let reverse(n) = valof
$( let r=0
while n>0
$( r := 10*r + n rem 10
n := n/10
$)
resultis r
$)
 
let start() be
$( let prime = vec 999
sieve(prime, 999)
for i = 2 to 500
if i!prime & reverse(i)!prime do
writef("%N ",i)
wrch('*N')
$)</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</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 248 ⟶ 396:
printf("\nCount = %u\n", count);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 260 ⟶ 408:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">sieve = proc (max: int) returns (array[bool])
prime: array[bool] := array[bool]$fill(0,max+1,true)
prime[0] := false
Line 291 ⟶ 439:
end
end
end start_up</langsyntaxhighlight>
{{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</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
const PRIME_LIMIT := 999;
const LIMIT := 500;
 
var prime: uint8[PRIME_LIMIT + 1];
typedef P is @indexof prime;
sub sieve() is
prime[0] := 0;
prime[1] := 0;
MemSet(&prime[2], 0xFF, @bytesof prime-2);
var p: P := 2;
while p*p <= PRIME_LIMIT loop
if prime[p] != 0 then
var c := p*p;
while c <= PRIME_LIMIT loop
prime[c] := 0;
c := c + p;
end loop;
end if;
p := p + 1;
end loop;
end sub;
 
sub reverse(n: P): (r: P) is
r := 0;
while n>0 loop
r := 10*r + n%10;
n := n/10;
end loop;
end sub;
 
sieve();
var n: P := 1;
while n <= LIMIT loop
if prime[n] != 0 and prime[reverse(n)] != 0 then
print_i32(n as uint32);
print_char(' ');
end if;
n := n + 1;
end loop;
print_nl();</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</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| PrimTrial}}
{{Trans|Ring}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Find_prime_n_for_that_reversed_n_is_also_prime;
 
Line 347 ⟶ 540:
Writeln('done...');
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>working...
Line 358 ⟶ 551:
found 34 primes
done...</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc reverse n .
while n > 0
r = r * 10 + n mod 10
n = n div 10
.
return r
.
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
fastfunc nextprim prim .
repeat
prim += 1
until isprim prim = 1
.
return prim
.
prim = 2
while prim < 500
if isprim reverse prim = 1
write prim & " "
.
prim = nextprim prim
.
</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
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Reversible Primes. Nigel Galloway: March 22nd., 2021
let emirp2=let rec fN g=function |0->g |n->fN(g*10+n%10)(n/10) in primes32()|>Seq.filter(fN 0>>isPrime)
emirp2|>Seq.takeWhile((>)500)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{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
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel math math.primes sequences ;
 
: reverse-digits ( 123 -- 321 )
Line 379 ⟶ 612:
499 primes-upto [ reverse-digits prime? ] filter
dup length "Found %d reverse primes < 500.\n\n" printf
10 group [ [ "%4d" printf ] each nl ] each nl</langsyntaxhighlight>
{{out}}
<pre>
Line 392 ⟶ 625:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: not-prime! ( n -- ) here + 1 swap c! ;
 
Line 433 ⟶ 666:
 
main
bye</langsyntaxhighlight>
 
{{out}}
Line 446 ⟶ 679:
=={{header|FreeBASIC}}==
Use one of the primality testing algorithms as an include as I can't be bothered putting these in all the time.
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function isbackprime( byval n as integer ) as boolean
Line 462 ⟶ 695:
if isbackprime(n) then print n;" ";
next n
print</langsyntaxhighlight>
{{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</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">select[primes[2,500], {|n| isPrime[parseInt[join["", reverse[integerDigits[n]]]]]}]</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]
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 523 ⟶ 763:
}
fmt.Printf("\n\n%d such primes found.\n", len(reversedPrimes))
}</langsyntaxhighlight>
 
{{out}}
Line 537 ⟶ 777:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime, primes)
Line 574 ⟶ 814:
widths
)
rows</langsyntaxhighlight>
{{Out}}
<pre>Reversible primes below 500:
Line 582 ⟶ 822:
359 373 383 389</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> (#~ 1 p: |.&.":"0) i.&.(p:inv) 500
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</syntaxhighlight>
 
=={{header|jq}}==
Line 588 ⟶ 831:
 
Using the definition of is_prime at [[Erd%C5%91s-primes#jq]]:
<langsyntaxhighlight lang="jq"># Generate a stream of reversible primes.
# If . is null the stream is unbounded;
# otherwise only integers less than . are considered.
Line 598 ⟶ 841:
 
"Primes under 500 which are also primes when the digits are reversed:",
(500 | reversible_primes)</langsyntaxhighlight>
{{out}}
<pre>
Line 639 ⟶ 882:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
let
Line 654 ⟶ 897:
println("Total found: $pcount")
end
</langsyntaxhighlight>{{out}}
<pre>
Reversible primes between 0 and 500:
Line 663 ⟶ 906:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Select[Range[499], PrimeQ[#] \[And] PrimeQ[IntegerReverse[#]] &]</langsyntaxhighlight>
{{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}</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find primes whose reversed digits are also prime
local function isPrime( p )
if p <= 1 or p % 2 == 0 then
return p == 2
else
local prime = true
local i = 3
local rootP = math.floor( math.sqrt( p ) )
while i <= rootP and prime do
prime = p % i ~= 0
i = i + 2
end
return prime
end
end
local function reverseDigits( n )
return tonumber( string.reverse( tostring( n ) ) )
end
local count = 0
for n = 1,500 do
if isPrime( n ) then
if isPrime( reverseDigits( n ) ) then
count = count + 1
io.write( string.format( "%3d", n ), count % 10 == 0 and "\n" or " " )
end
end
end
io.write( "\nFound ", count, " reversable primes up to 500" )
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|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(1000)
VECTOR VALUES FMT = $I3*$
PRIME(0)=0B
PRIME(1)=0B
THROUGH INIT, FOR P=2, 1, P.GE.1000
INIT PRIME(P)=1B
 
THROUGH SIEVE, FOR P=2, 1, P.GE.32
THROUGH SIEVE, FOR C=P*P, P, C.GE.1000
SIEVE PRIME(C)=0B
THROUGH TEST, FOR N=2, 1, N.GE.500
WHENEVER .NOT. PRIME(N), TRANSFER TO TEST
NN=N
R=0
RVRSE WHENEVER NN.G.0
NXT=NN/10
R=R*10+NN-NXT*10
NN=NXT
TRANSFER TO RVRSE
END OF CONDITIONAL
WHENEVER .NOT. PRIME(R), TRANSFER TO TEST
PRINT FORMAT FMT,N
TEST CONTINUE
END OF PROGRAM</syntaxhighlight>
{{out}}
<pre style='height:50ex;'> 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</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE ReversePrime;
FROM InOut IMPORT WriteCard, WriteLn;
 
Line 718 ⟶ 1,067:
END;
WriteLn();
END ReversePrime.</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 31
Line 728 ⟶ 1,077:
=={{header|Nim}}==
 
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
const
Line 757 ⟶ 1,106:
stdout.write ($n).align(3)
stdout.write if (i + 1) mod 10 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 764 ⟶ 1,113:
167 179 181 191 199 311 313 337 347 353
359 373 383 389 </pre>
 
=={{header|OCaml}}==
Using the function <code>seq_primes</code> from [[Extensible prime generator#OCaml]]:
<syntaxhighlight lang="ocaml">let int_reverse =
let rec loop m n =
if n < 10 then m + n else loop ((m + n mod 10) * 10) (n / 10)
in loop 0
 
let is_prime n =
let not_divisible x = n mod x <> 0 in
seq_primes |> Seq.take_while (fun x -> x * x <= n) |> Seq.for_all not_divisible
 
let () =
seq_primes |> Seq.filter (fun p -> is_prime (int_reverse p))
|> Seq.take_while ((>) 500) |> Seq.iter (Printf.printf " %u") |> print_newline</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</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Type Tboolarr = array Of boolean;
 
Const MAX = 1000;
Function SieveOfEratosthenes(limit: Integer): Tboolarr;
Var
sieve: Tboolarr;
i, j: Integer;
Begin
SetLength(sieve, limit + 1);
sieve[2] := True;
For i := 3 To limit Do
sieve[i] := (i Mod 2 <> 0);
For i := 3 To Trunc(Sqrt(limit)) Do
Begin
If sieve[i] Then
Begin
j := i * i;
While j <= limit Do
Begin
sieve[j] := False;
Inc(j, 2 * i);
End;
End;
End;
SieveOfEratosthenes := sieve;
End;
 
Function ReverseNumber(number: Integer): Integer;
Var
reversed: Integer;
Begin
reversed := 0;
While number <> 0 Do
Begin
reversed := reversed * 10 + (number Mod 10);
number := number Div 10;
End;
ReverseNumber := reversed;
End;
 
Var
primes: Tboolarr;
i: Integer;
Begin
primes := SieveOfEratosthenes(MAX);
For i := 2 To 500 Do
Begin
If primes[i] And Primes[ReverseNumber(i)] Then
Write(i,' ');
End;
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
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'max';
Line 780 ⟶ 1,206:
my($limit, @rp) = 500;
is_prime($_) and is_prime(reverse $_) and push @rp, $_ for 1..$limit;
print @rp . " reversible primes < $limit:\n" . pp(@rp);</langsyntaxhighlight>
{{out}}
<pre>34 reversible primes < 500:
Line 788 ⟶ 1,214:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">rp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">to_integer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">))))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">args</span><span style="color: #0000FF;">)</span>
Line 797 ⟶ 1,223:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">500</span><span style="color: #0000FF;">,</span><span style="color: #008000;">":\n%s"</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">":\n%s"</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10_000_000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 811 ⟶ 1,237:
82,439 reverse primes < 10,000,000 found.
</pre>
 
 
=={{header|Python}}==
<syntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
 
def isBackPrime(n):
if not isPrime(n):
return False
m = 0
while n:
m *= 10
m += n % 10
n //= 10
return isPrime(m)
 
if __name__ == '__main__':
for n in range(2, 499):
if isBackPrime(n):
print(n, 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</pre>
 
 
=={{header|PILOT}}==
<langsyntaxhighlight lang="pilot">C :n=1
*number
C :p=n
Line 849 ⟶ 1,303:
J (i<p/2):*div
C :pr=1
E :</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>2
Line 885 ⟶ 1,339:
383
389</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">reversePrimes: procedure options(main);
declare prime(1:999) bit;
sieve: procedure;
declare (i,j,hi) fixed;
hi = hbound(prime,1);
prime(1) = '0'b;
do i=2 to hi; prime(i) = '1'b; end;
do i=2 to sqrt(hi);
do j=i*i to hi by i; prime(j) = '0'b; end;
end;
end sieve;
reverse: procedure(nn) returns(fixed);
declare (n, nn, r) fixed;
r=0;
do n=nn repeat(n/10) while (n>0);
r = 10*r + mod(n,10);
end;
return(r);
end reverse;
declare (n, found) fixed;
call sieve;
found = 0;
do n=1 to 499;
if prime(n) & prime(reverse(n)) then do;
put edit(n) (F(4));
found = found + 1;
if mod(found,20) = 0 then put skip;
end;
end;
put skip list('Reverse primes found:',found);
end reversePrimes;</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
Reverse primes found: 34</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* FIND PRIMES THAT ARE STILL PRIME WHEN THEIR DIGITS ARE REVERSED */
 
/* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
/* I/O ROUTINES */
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* RETURNS TRUE IF N IS PRIME, FALSE OTHERWISE, USES TRIAL DIVISION */
IS$PRIME: PROCEDURE( N )BYTE;
DECLARE N ADDRESS;
DECLARE PRIME BYTE;
IF N < 3 THEN PRIME = N = 2;
ELSE IF N MOD 3 = 0 THEN PRIME = N = 3;
ELSE IF N MOD 2 = 0 THEN PRIME = 0;
ELSE DO;
DECLARE ( F, F2, TO$NEXT ) ADDRESS;
PRIME = 1;
F = 5;
F2 = 25;
TO$NEXT = 24; /* NOTE: ( 2N + 1 )^2 - ( 2N - 1 )^2 = 8N */
DO WHILE F2 <= N AND PRIME;
PRIME = N MOD F <> 0;
F = F + 2;
F2 = F2 + TO$NEXT;
TO$NEXT = TO$NEXT + 8;
END;
END;
RETURN PRIME;
END IS$PRIME;
 
REVERSE: PROCEDURE( N )ADDRESS; /* RETURNS THE REVERSED DIGITS OF N */
DECLARE N ADDRESS;
DECLARE ( R, V ) ADDRESS;
V = N;
R = V MOD 10;
DO WHILE( ( V := V / 10 ) > 0 );
R = ( R * 10 ) + ( V MOD 10 );
END;
RETURN R;
END REVERSE ;
 
/* FIND THE NUMBERS UP TO 500 */
 
DECLARE ( I, COUNT ) ADDRESS;
 
COUNT = 0;
DO I = 1 TO 500;
IF IS$PRIME( I ) THEN DO;
IF IS$PRIME( REVERSE( I ) ) THEN DO;
IF I < 10 THEN CALL PR$CHAR( ' ' );
IF I < 100 THEN CALL PR$CHAR( ' ' );
CALL PR$NUMBER( I );
IF ( COUNT := COUNT + 1 ) MOD 20 = 0 THEN CALL PR$NL;
ELSE CALL PR$CHAR( ' ' );
END;
END;
END;
 
EOF
</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
</pre>
 
=={{header|Quackery}}==
Line 890 ⟶ 1,468:
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> 1000 eratosthenes
 
[ number$ reverse $->n drop ] is revnum ( n --> n )
Line 902 ⟶ 1,480:
[ i^ join ] ]
witheach [ number$ nested join ]
60 wrap$</langsyntaxhighlight>
 
{{out}}
Line 911 ⟶ 1,489:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>unit sub MAIN ($limit = 500);
say "{+$_} reversible primes < $limit:\n{$_».fmt("%" ~ $limit.chars ~ "d").batch(10).join("\n")}",
with ^$limit .grep: { .is-prime and .flip.is-prime }</langsyntaxhighlight>
{{out}}
<pre>34 reversible primes < 500:
Line 922 ⟶ 1,500:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program counts/displays the number of reversed primes under a specified number N.*/
parse arg n cols . /*get optional number of primes to find*/
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
Line 961 ⟶ 1,539:
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 980 ⟶ 1,558:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,008 ⟶ 1,586:
see nl + "found " + num + " primes" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,018 ⟶ 1,596:
found 34 primes
done...
</pre>
 
=={{header|RPL}}==
This program uses the words <code>RVSTR</code> and <code>BPRIM?</code>, respectively made to [[Reverse_a_string#RPL|revert a string]] and to [[Primality_by_trial_division#RPL|test primality by trial division]]
≪ { } 1 499 '''FOR''' n
'''IF''' n R→B '<span style="color:blue">BPRIM?</span> THEN'''
'''IF''' n →STR <span style="color:blue">RVSTR</span> STR→ R→B <span style="color:blue">BPRIM?</span> '''THEN''' n + '''END'''
'''END'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
 
====Straightforward approach====
Numbers are here reversed without any string conversion.
{{works with|HP|49}}
« { }
1 499 '''FOR''' n
'''IF''' n ISPRIME? '''THEN'''
n 0
'''WHILE''' OVER '''REPEAT'''
SWAP 10 IDIV2 ROT 10 * +
'''END''' NIP
'''IF''' ISPRIME? '''THEN''' n + '''END'''
'''END'''
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 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 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="Ruby">p Prime.each(500).select{|pr| pr.digits.join.to_i.prime? }
</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]
</pre>
=={{header|Rust}}==
<syntaxhighlight lang="Rust">
use prime_tools ;
 
fn myreverse( n : u32 ) -> u32 {
let forward : String = n.to_string( ) ;
let numberstring = &forward[..] ;
let mut reversed : String = String::new( ) ;
for c in numberstring.chars( ).rev( ) {
reversed.push( c ) ;
}
*&reversed[..].parse::<u32>( ).unwrap( )
}
 
fn main() {
let mut reversible_primes : Vec<u32> = Vec::new( ) ;
for num in 2..=500 {
if prime_tools::is_u32_prime( num ) && prime_tools::is_u32_prime(
myreverse( num )) {
reversible_primes.push( num ) ;
}
}
println!("{:?}" , reversible_primes ) ;
}</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]
</pre>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
Line 1,068 ⟶ 1,710:
writeln;
writeln("Found " <& count <& " reverse primes < 500.");
end func;</langsyntaxhighlight>
{{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 reverse primes < 500.
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">say primes(500).grep { .reverse.is_prime }</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]
</pre>
 
Line 1,078 ⟶ 1,727:
{{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 1,098 ⟶ 1,745:
}
System.print("Primes under 500 which are also primes when the digits are reversed:")
Fmt.tprint("$3d", reversedPrimes, 17)
for (chunk in Lst.chunks(reversedPrimes, 17)) Fmt.print("$3d", chunk)
System.print("\n%(reversedPrimes.count) such primes found.")</langsyntaxhighlight>
 
{{out}}
Line 1,111 ⟶ 1,758:
 
=={{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,141 ⟶ 1,788:
IntOut(0, Count);
Text(0, " reversible primes found.");
]</langsyntaxhighlight>
 
{{out}}
1,969

edits