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

m
syntax highlighting fixup automation
(Added Arturo implementation)
m (syntax highlighting fixup automation)
Line 6:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 18:
L(n) 1..499
I is_prime(n) & is_prime(Int(reversed(String(n))))
print(n, end' ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 27:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC IsPrimeAndItsReverse(INT i BYTE ARRAY primes)
Line 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 71:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Reverse_Prime is
Line 125:
New_Line;
Put_Line (Count'Image & " primes.");
end Reverse_Prime;</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 31
Line 135:
 
=={{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 184:
write( i_w := 1, s_w := 0, "Found ", pCount, " reversable primes below ", MAX_NUMBER )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 193:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">print
select 1..499 'x ->
and? [prime? x][prime? to :integer reverse to :string x]</langsyntaxhighlight>
 
{{out}}
Line 202:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_PRIME_N_FOR_THAT_REVERSED_N_IS_ALSO_PRIME.AWK
BEGIN {
Line 232:
return( substr(str,start,1) revstr(str,start-1) )
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 243:
 
=={{header|BASIC}}==
<langsyntaxhighlight 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
Line 253:
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</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11
Line 264:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let sieve(prime, n) be
Line 295:
writef("%N ",i)
wrch('*N')
$)</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|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 335:
printf("\nCount = %u\n", count);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 347:
 
=={{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 378:
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}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
const PRIME_LIMIT := 999;
Line 424:
n := n + 1;
end loop;
print_nl();</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>
Line 431:
{{libheader| PrimTrial}}
{{Trans|Ring}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Find_prime_n_for_that_reversed_n_is_also_prime;
 
Line 479:
Writeln('done...');
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>working...
Line 493:
=={{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>
Line 504:
=={{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 511:
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 524:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: not-prime! ( n -- ) here + 1 swap c! ;
 
Line 565:
 
main
bye</langsyntaxhighlight>
 
{{out}}
Line 578:
=={{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 594:
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}}==
<langsyntaxhighlight lang="frink">select[primes[2,500], {|n| isPrime[parseInt[join["", reverse[integerDigits[n]]]]]}]</langsyntaxhighlight>
{{out}}
<pre>
Line 605:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 662:
}
fmt.Printf("\n\n%d such primes found.\n", len(reversedPrimes))
}</langsyntaxhighlight>
 
{{out}}
Line 676:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime, primes)
Line 713:
widths
)
rows</langsyntaxhighlight>
{{Out}}
<pre>Reversible primes below 500:
Line 727:
 
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 737:
 
"Primes under 500 which are also primes when the digits are reversed:",
(500 | reversible_primes)</langsyntaxhighlight>
{{out}}
<pre>
Line 778:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
let
Line 793:
println("Total found: $pcount")
end
</langsyntaxhighlight>{{out}}
<pre>
Reversible primes between 0 and 500:
Line 802:
 
=={{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|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(1000)
Line 834:
PRINT FORMAT FMT,N
TEST CONTINUE
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'> 2
Line 872:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE ReversePrime;
FROM InOut IMPORT WriteCard, WriteLn;
 
Line 922:
END;
WriteLn();
END ReversePrime.</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 31
Line 932:
=={{header|Nim}}==
 
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
const
Line 961:
stdout.write ($n).align(3)
stdout.write if (i + 1) mod 10 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 971:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'max';
Line 984:
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 992:
 
=={{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 1,001:
<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 1,018:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
Line 1,039:
for n in range(2, 499):
if isBackPrime(n):
print(n, end=' ');</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>
Line 1,045:
 
=={{header|PILOT}}==
<langsyntaxhighlight lang="pilot">C :n=1
*number
C :p=n
Line 1,081:
J (i<p/2):*div
C :pr=1
E :</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>2
Line 1,119:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">reversePrimes: procedure options(main);
declare prime(1:999) bit;
Line 1,153:
put skip list('Reverse primes found:',found);
end reversePrimes;</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157
Line 1,163:
<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 1,175:
[ i^ join ] ]
witheach [ number$ nested join ]
60 wrap$</langsyntaxhighlight>
 
{{out}}
Line 1,184:
 
=={{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 1,195:
 
=={{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 1,234:
#= # + 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 1,253:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,281:
see nl + "found " + num + " primes" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,294:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
Line 1,341:
writeln;
writeln("Found " <& count <& " reverse primes < 500.");
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,349:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say primes(500).grep { .reverse.is_prime }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,359:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/seq" for Lst
Line 1,379:
System.print("Primes under 500 which are also primes when the digits are reversed:")
for (chunk in Lst.chunks(reversedPrimes, 17)) Fmt.print("$3d", chunk)
System.print("\n%(reversedPrimes.count) such primes found.")</langsyntaxhighlight>
 
{{out}}
Line 1,391:
 
=={{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,421:
IntOut(0, Count);
Text(0, " reversible primes found.");
]</langsyntaxhighlight>
 
{{out}}
10,333

edits