Find prime n such that reversed n is also prime: Difference between revisions
Find prime n such that reversed n is also prime (view source)
Revision as of 12:57, 27 August 2022
, 1 year agosyntax highlighting fixup automation
Drkameleon (talk | contribs) (Added Arturo implementation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 6:
=={{header|11l}}==
<
I a == 2
R 1B
Line 18:
L(n) 1..499
I is_prime(n) & is_prime(Int(reversed(String(n))))
print(n, end' ‘ ’)</
{{out}}
Line 27:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
BYTE FUNC IsPrimeAndItsReverse(INT i BYTE ARRAY primes)
Line 61:
OD
PrintF("%E%EThere are %I primes",count)
RETURN</
{{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}}==
<
procedure Reverse_Prime is
Line 125:
New_Line;
Put_Line (Count'Image & " primes.");
end Reverse_Prime;</
{{out}}
<pre> 2 3 5 7 11 13 17 31
Line 135:
=={{header|ALGOL W}}==
<
% 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.</
{{out}}
<pre>
Line 193:
=={{header|Arturo}}==
<
select 1..499 'x ->
and? [prime? x][prime? to :integer reverse to :string x]</
{{out}}
Line 202:
=={{header|AWK}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 243:
=={{header|BASIC}}==
<
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</
{{out}}
<pre> 2 3 5 7 11
Line 264:
=={{header|BCPL}}==
<
let sieve(prime, n) be
Line 295:
writef("%N ",i)
wrch('*N')
$)</
{{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}}==
<
#include <stdio.h>
Line 335:
printf("\nCount = %u\n", count);
return 0;
}</
{{out}}
Line 347:
=={{header|CLU}}==
<
prime: array[bool] := array[bool]$fill(0,max+1,true)
prime[0] := false
Line 378:
end
end
end start_up</
{{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}}==
<
const PRIME_LIMIT := 999;
Line 424:
n := n + 1;
end loop;
print_nl();</
{{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">
program Find_prime_n_for_that_reversed_n_is_also_prime;
Line 479:
Writeln('done...');
readln;
end.</
{{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#)]
<
// 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>
{{out}}
<pre>
Line 504:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
: 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</
{{out}}
<pre>
Line 524:
=={{header|Forth}}==
{{works with|Gforth}}
<
: not-prime! ( n -- ) here + 1 swap c! ;
Line 565:
main
bye</
{{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.
<
function isbackprime( byval n as integer ) as boolean
Line 594:
if isbackprime(n) then print n;" ";
next n
print</
{{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}}==
<
{{out}}
<pre>
Line 605:
=={{header|Go}}==
<
import "fmt"
Line 662:
}
fmt.Printf("\n\n%d such primes found.\n", len(reversedPrimes))
}</
{{out}}
Line 676:
=={{header|Haskell}}==
<
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime, primes)
Line 713:
widths
)
rows</
{{Out}}
<pre>Reversible primes below 500:
Line 727:
Using the definition of is_prime at [[Erd%C5%91s-primes#jq]]:
<
# 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)</
{{out}}
<pre>
Line 778:
=={{header|Julia}}==
<
let
Line 793:
println("Total found: $pcount")
end
</
<pre>
Reversible primes between 0 and 500:
Line 802:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{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}}==
<
BOOLEAN PRIME
DIMENSION PRIME(1000)
Line 834:
PRINT FORMAT FMT,N
TEST CONTINUE
END OF PROGRAM</
{{out}}
<pre style='height:50ex;'> 2
Line 872:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 922:
END;
WriteLn();
END ReversePrime.</
{{out}}
<pre> 2 3 5 7 11 13 17 31
Line 932:
=={{header|Nim}}==
<
const
Line 961:
stdout.write ($n).align(3)
stdout.write if (i + 1) mod 10 == 0: '\n' else: ' '
echo()</
{{out}}
Line 971:
=={{header|Perl}}==
{{libheader|ntheory}}
<
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);</
{{out}}
<pre>34 reversible primes < 500:
Line 992:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,018:
=={{header|Python}}==
<
def isPrime(n):
Line 1,039:
for n in range(2, 499):
if isBackPrime(n):
print(n, end=' ');</
{{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}}==
<
*number
C :p=n
Line 1,081:
J (i<p/2):*div
C :pr=1
E :</
{{out}}
<pre style='height:50ex;'>2
Line 1,119:
=={{header|PL/I}}==
<
declare prime(1:999) bit;
Line 1,153:
put skip list('Reverse primes found:',found);
end reversePrimes;</
{{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]].
<
[ number$ reverse $->n drop ] is revnum ( n --> n )
Line 1,175:
[ i^ join ] ]
witheach [ number$ nested join ]
60 wrap$</
{{out}}
Line 1,184:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
say "{+$_} reversible primes < $limit:\n{$_».fmt("%" ~ $limit.chars ~ "d").batch(10).join("\n")}",
with ^$limit .grep: { .is-prime and .flip.is-prime }</
{{out}}
<pre>34 reversible primes < 500:
Line 1,195:
=={{header|REXX}}==
<
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,253:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,281:
see nl + "found " + num + " primes" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,294:
=={{header|Seed7}}==
<
const func boolean: isPrime (in integer: number) is func
Line 1,341:
writeln;
writeln("Found " <& count <& " reverse primes < 500.");
end func;</
{{out}}
<pre>
Line 1,349:
=={{header|Sidef}}==
<
{{out}}
<pre>
Line 1,359:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<
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.")</
{{out}}
Line 1,391:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 1,421:
IntOut(0, Count);
Text(0, " reversible primes found.");
]</
{{out}}
|