Emirp primes: Difference between revisions

m
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
 
(13 intermediate revisions by 11 users not shown)
Line 29:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F reversed(Int =n)
V result = 0
L
Line 70:
I ++count == 10000
print(‘The 10000th emirp: ’n)
L.break</langsyntaxhighlight>
 
{{out}}
Line 83:
he solution uses the package Miller_Rabin from the [[Miller-Rabin primality test]].
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Miller_Rabin;
 
procedure Emirp_Gen is
Line 151:
end loop;
Ada.Text_IO.Put_Line(Num'Image(Current));
end Emirp_Gen;</langsyntaxhighlight>
 
{{out}}
Line 163:
Allows the user to specify the from and to range values or ordinals on the command line. The sieve size can also be specified. As suggested by the Fortran sample, from = to is treated as a special case for labeling the output.
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68"># parse the command line - ignore errors #
INT emirp from := 1; # lowest emirp required #
INT emirp to := 10; # highest emirp required #
Line 242:
OD
FI;
print( ( newline ) )</langsyntaxhighlight>
{{out}}
a68g emirpPrimes.a68 - FROM 1 TO 20
Line 259:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">emirps: function [upto][
result: new []
loop range .step: 2 11 upto 'x [
Line 284:
print ""
print "The 10000th emirp:"
print lst\9999</langsyntaxhighlight>
 
{{out}}
Line 298:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetBatchLines, -1
p := 1
Loop, 20 {
Line 358:
r := A_LoopField r
return r
}</langsyntaxhighlight>
{{Output}}
<pre>First twenty emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Line 368:
 
cat emirp.awk
<syntaxhighlight lang="awk">
<lang AWK>
function is_prime(n, p)
{
Line 414:
printf("\n")
}
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 422:
948349
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> REM Taking advantage of inbuilt assembler to implement ultra fast Prime tester!
DIM P% 127:[OPT 0
.F% pop eax:mov eax,0:.T% ret:.M% mov eax,ecx:xor edx,edx:div ebx:cmp edx,0:jz F%:ret
.E% mov ebx,2:call M%:mov ebx,3:call M%:mov ebx,5:.W% mov edx,ebx:imul edx,ebx
cmp edx,ecx:jg T%:call M%:add ebx,2:call M%:add ebx,4:jmp W%:]
DEF FNIsPrime(C%)=USRE%
 
N%=0
P%=11
PRINT "First 20 emirps are:";
WHILE N%<10000
P%+=2
IF FNIsPrime(P%) THEN
R%=VALFNRev(STR$P%)
IF P%<>R% IF FNIsPrime(R%) THEN
IF N%<20 OR (P%>7699 AND P%<8001) PRINT " ";P%;
N%+=1
IF N%=20 PRINT '"Emirps between 7700 and 8000 are:";
ENDIF
ENDIF
ENDWHILE
PRINT '"The 10,000th emirp is: ";P%
END
 
DEF FNRev(n$)
Q%=!^n$
L%=LENn$-1
FOR I%=0 TO L%/2 SWAP Q%?I%, Q%?(L%-I%) NEXT
=n$</syntaxhighlight>
{{out}}
<pre>First 20 emirps are: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Emirps between 7700 and 8000 are: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
The 10,000th emirp is: 948349</pre>
 
=={{header|C}}==
Note the unusual commandline argument parsing to sastisfy the "invoke three times" magic requirement.
<langsyntaxhighlight lang="c">#include <stdio.h>
 
typedef unsigned uint;
Line 476 ⟶ 512:
putchar('\n');
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 487 ⟶ 523:
</pre>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|7}}
<langsyntaxhighlight lang="csharp">using static System.Console;
using System;
using System.Linq;
Line 558 ⟶ 594:
return reverse;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 572 ⟶ 608:
 
=={{header|C++}}==
<langsyntaxhighlight Cpplang="cpp">#include <vector>
#include <iostream>
#include <algorithm>
Line 632 ⟶ 668:
 
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Line 652 ⟶ 688:
===Using biginteger's isProbablePrime()===
The isProbablePrime() method performs a Miller-Rabin primality test to within a given certainty.
<langsyntaxhighlight lang="clojure">(defn emirp? [v]
(let [a (biginteger v)
b (biginteger (clojure.string/reverse (str v)))]
Line 664 ⟶ 700:
(println "10,000: " (nth (filter emirp? (iterate inc 0)) 9999))
 
</syntaxhighlight>
</lang>
{{out}}
<pre>first20: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Line 674 ⟶ 710:
 
It uses a primitive prime function found in http://www.rosettacode.org/wiki/Primality_by_trial_division, not optimized at all.
<langsyntaxhighlight Lisplang="lisp">(defun primep (n)
"Is N prime?"
(and (> n 1)
Line 702 ⟶ 738:
(format t "~%Emirps between 7700 and 8000: ") (emirp :start 7700 :end 8000 :print-all t)
(format t "~%The 10,000'th emirp: ") (emirp :count 10000 :print-all nil) )
</syntaxhighlight>
</lang>
 
{{out}}
Line 710 ⟶ 746:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">bool isEmirp(uint n) pure nothrow @nogc {
bool isPrime(in uint n) pure nothrow @nogc {
if (n == 2 || n == 3)
Line 743 ⟶ 779:
iota(7_700, 8_001).filter!isEmirp);
writeln("10000th: ", uints.filter!isEmirp.drop(9_999).front);
}</langsyntaxhighlight>
{{out}}
<pre>First 20:
Line 753 ⟶ 789:
 
===Sieve-Based Version===
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.bitmanip;
 
/// Not extendible Sieve of Eratosthenes.
Line 795 ⟶ 831:
iota(7_700, 8_001).filter!isEmirp);
writeln("10000th: ", uints.filter!isEmirp.drop(9_999).front);
}</langsyntaxhighlight>
The output is the same. With ldc2 compiler the run-time is about 0.06 seconds.
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Emirp_primes#Pascal Pascal].
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func isemirp n .
if isprim n = 0
return 0
.
m = n
while m > 0
d = m mod 10
m = m div 10
rev = rev * 10 + d
.
if rev = n
return 0
.
return isprim rev
.
m = 2
write "First 20 emirps: "
while cnt < 20
if isemirp m = 1
write m & " "
cnt += 1
.
m += 1
.
print ""
write "Emirps between 7700 8000: "
for m = 7700 to 8000
if isemirp m = 1
write m & " "
.
.
print ""
m = 2
cnt = 0
repeat
cnt += isemirp m
until cnt = 10000
m += 1
.
print "The 10000th emirp: " & m
</syntaxhighlight>
{{out}}
<pre>
First 20 emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Emirps between 7700 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
The 10000th emirp: 948349
</pre>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Emirp do
defp prime?(2), do: true
defp prime?(n) when n<2 or rem(n,2)==0, do: false
Line 833 ⟶ 929:
end
 
Emirp.task</langsyntaxhighlight>
 
{{out}}
Line 845 ⟶ 941:
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Generate emirps. Nigel Galloway: November 19th., 2017
let emirp =
Line 851 ⟶ 947:
let fG n g = n<>g && isPrime g
primes32() |> Seq.filter (fun n -> fG n (fN n 0))
</syntaxhighlight>
</lang>
 
===The Task===
<langsyntaxhighlight lang="fsharp">
emirps |> (Seq.take 20) |> Seq.iter (printf "%d ")
</syntaxhighlight>
</lang>
{{out}}
<pre>
13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
</pre>
<langsyntaxhighlight lang="fsharp">
emirps |> Seq.skipWhile (fun n->n<7700) |> Seq.takeWhile (fun n->n<=8000) |> Seq.iter (printf "%d ")
</syntaxhighlight>
</lang>
{{out}}
<pre>
7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
</pre>
<langsyntaxhighlight lang="fsharp">
printfn "%d" (Seq.item 9999 emirps)
</syntaxhighlight>
</lang>
{{out}}
<pre>
948349
</pre>
<langsyntaxhighlight lang="fsharp">
// count # of emirps with n = 2 to 7 digits. Nigel Galloway: August 8th., 2018
let n=emirp |> Seq.takeWhile(fun n->n<10000000) |> Seq.countBy(fun n->match n with |n when n>999999->7
Line 884 ⟶ 980:
|_ ->2)
for n,g in n do printfn "%d -> %d" n g
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 897 ⟶ 993:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: io kernel lists lists.lazy math.extras math.parser
math.primes sequences ;
FROM: prettyprint => . pprint ;
Line 927 ⟶ 1,023:
part1 nl part2 nl part3 ;
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 942 ⟶ 1,038:
=={{header|Forth}}==
Keeps a pair of numbers on the stack to represent a generator. This generator approach means that all tasks start from the first Emirp (13), even task 2.
<syntaxhighlight lang="forth">
<lang Forth>
#! /usr/bin/gforth-fast
 
Line 1,019 ⟶ 1,115:
task1 task2 task3
cr bye
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,036 ⟶ 1,132:
For factoring numbers up to the 32-bit two's complement integer limit, the table need not be large, and it can easily enough be stored as a collection of sixteen and thirty-two bit numbers to save some space. Accessing an array PRIME(i) can be made a function GETPRIME(i) without a change in syntax (as needed in pascal: Prime[i] for an array, GetPrime(i) for a function), at least for reading. So, instead of 4792x4 = 19168 bytes, 12144 are needed, to set against the additional code complexity. These days, this is a difference of small importance. Actually, a further value is needed to hold Prime(4793) = 46349. Function ISPRIME does not determine its stepping point via the near universal usage of SQRT(n). If calculated in double precision this will give acceptable results for a 32-bit integer, but I have been burnt by an ad-hoc calculation nDgits = LOG10(x) + 1 failing for x = 10 because Log10(10) = 0·9999etc. which may well round to one, but truncates to zero. So, a SQRT-free demonstration, needed if the MOD function were unavailable. Actually, if P(i) is the last factor to be checked, this suffices up to the square of P(i + 1), not P(i). But this bound is only useful when successive numbers are being tested; for an individual factorisation it is too messy.
 
The initial version ran very slowly once past the first run, and this prompted some instrumentation, the addition of counters for the invocations. It transpired that GETPRIME(i) was being invoked thousands of millions of times... Once again, a N<sup>2</sup> process is to be avoided, here when NEXTPRIME(n) was stepping linearly along the array of primes (in the hope of knowing the next prime along without having to recalculate it) and being invoked many times to do so. This was fixed by introducing a binary search, the list of primes being of course in order. The early version of NEXTPRIME(n) also did not attempt to save new primes, as it might be invoked with a value well beyond the end of the table and the next value on from ''n'' might be past many lesser primes. But by working on from PRIME(NP) up to ''n'' they can be found and saved along the way. Saving new primes in NEXTPRIME meant that GETPRIME should no longer itself attempt saving, as it is invoking NEXTPRIME. Mutual recursion is all very well, but organisation is important also. <langsyntaxhighlight Fortranlang="fortran"> MODULE BAG !A mixed assortment.
INTEGER MSG !I/O unit number to share about.
INTEGER PF16LIMIT,PF32LIMIT,NP !Know that P(3512) = 32749, the last within two's complement 16-bit integers.
Line 1,209 ⟶ 1,305:
CALL EMIRP(10,10000,10000, 1,1000000) !Of three separate invocations.
 
END !Whee!</langsyntaxhighlight>
Output:
<pre>
Line 1,262 ⟶ 1,358:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function isPrime(n As UInteger) As Boolean
Line 1,323 ⟶ 1,419:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,338 ⟶ 1,434:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
isEmirp[x] :=
{
Line 1,357 ⟶ 1,453:
println["Range: " + emirps[7700, 8000]]
println["10000th: " + last[first[emirps[], 10000]]]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,364 ⟶ 1,460:
10000th: 948349
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
 
local fn ReverseNumber( n as NSUInteger ) as NSUInteger
NSInteger sum = 0
if n < 10 then exit fn = n
while ( n > 0 )
sum = 10 * sum + ( n mod 10 )
n /= 10
wend
end fn = sum
 
 
local fn IsEmirp( n as NSUInteger ) as BOOL
BOOL result = NO
NSUInteger r = fn ReverseNumber(n)
if r != n and fn IsPrime(n) and fn IsPrime(r) then result = YES
end fn = result
 
 
local fn GetEmirpPrimes
NSUInteger count = 0, i = 13
printf @"\nThe first 20 Emirp primes are:"
do
if fn IsEmirp(i) then printf @"%4lu\b", i : count++
i += 2
until ( count == 20 )
printf @"\n\nThe Emirp primes between 7700 and 8000 are:"
i = 7701
while ( i < 8000 )
if fn IsEmirp(i) then printf @"%5lu\b", i
i += 2
wend
i = 13 : count = 0
while (1)
if fn IsEmirp(i) then count++
if count = 10000 then exit while
i += 2
wend
printf @"\n\nThe 10,000th Emirp prime is: %lu", i
end fn
 
fn GetEmirpPrimes
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
The first 20 Emirp primes are:
13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
 
The Emirp primes between 7700 and 8000 are:
7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
 
The 10,000th Emirp prime is: 948349
</pre>
 
 
=={{header|Go}}==
Line 1,369 ⟶ 1,540:
 
As a side note, by using the same API as the prime number generator this also demonstrates how Go interfaces can be used (and note it doesn't require the existing code/package to know anything about the interface being defined).
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,552 ⟶ 1,723:
fmt.Println()
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,579 ⟶ 1,750:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class Emirp {
 
//trivial prime algorithm, sub in whatever algorithm you want
Line 1,634 ⟶ 1,805:
println(--x)
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 emirps:
Line 1,649 ⟶ 1,820:
{{Works with|primes|0.2.1.0}}
 
<langsyntaxhighlight lang="haskell">#!/usr/bin/env runghc
 
import Data.HashSet (HashSet, fromList, member)
Line 1,724 ⟶ 1,895:
"slice" -> print $ emirpSlice lo hi
"values" -> print $ emirpValues lo hi
_ -> usage</langsyntaxhighlight>
 
{{out}}
Line 1,740 ⟶ 1,911:
===List-based===
Using list-based incremental sieve from [[Sieve_of_Eratosthenes#With_Wheel|here]] and trial division from [[Primality_by_trial_division#Haskell|here]],
<langsyntaxhighlight lang="haskell"> λ> let emirp p = let q=(read.reverse.show) p in q /= p && noDivsBy primesW q
 
λ> take 20 . filter emirp $ primesW
Line 1,749 ⟶ 1,920:
 
λ> (!! (10000-1)) . filter emirp $ primesW
948349 -- 0.69 secs</langsyntaxhighlight>
 
=={{header|J}}==
 
'''Solution''':<langsyntaxhighlight lang="j"> emirp =: (] #~ ~: *. 1 p: ]) |.&.:":"0 NB. Input is array of primes</langsyntaxhighlight>
 
In other words: select numbers from the argument list whose decimal reverse is both different and prime and return those decimal reversed values as numbers. (For simplicity, we require that our argument be a list of prime numbers.)
 
'''Examples'''<langsyntaxhighlight lang="j"> /:~ emirp p: 2+i.75
13 17 31 37 71 73 79 97 113 311 701 733 743 751 761 941 953 971 983 991
 
Line 1,770 ⟶ 1,941:
NB. alternative approach (first emirp value would be at index 0):
9999 { /:~ emirp p:i.1e5
943849</langsyntaxhighlight>
 
=={{header|Java}}==
This implementation uses a slight optimization discussed in the talk page. It will not actually check the primality (forwards or backwards) for a number that starts or ends with the digits 2, 4, 5, 6, or 8 since no primes greater than 7 end with those digits.
<langsyntaxhighlight lang="java">public class Emirp{
//trivial prime algorithm, sub in whatever algorithm you want
Line 1,827 ⟶ 1,998:
System.out.println(--x);
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 emirps:
Line 1,838 ⟶ 2,009:
=={{header|JavaScript}}==
Script source
<langsyntaxhighlight lang="javascript">function isPrime(n) {
if (!(n % 2) || !(n % 3)) return 0;
 
Line 1,901 ⟶ 2,072:
}
}
</syntaxhighlight>
</lang>
 
Solution page
<langsyntaxhighlight lang="html"><!DOCTYPE html>
<html>
<head>
Line 1,913 ⟶ 2,084:
<div id="content"></div>
</body>
</html></langsyntaxhighlight>
{{out}}
<pre>13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Line 1,927 ⟶ 2,098:
 
'''Infrastructure: prime numbers'''
<langsyntaxhighlight lang="jq">def is_prime:
if . == 2 then true
else
Line 1,958 ⟶ 2,129:
| until( relatively_prime ; .[0] += 2) as $nextp
| ( $previous + [$nextp[0]] );
2, ([2,3] | recurse( next ) | .[-1]) ;</langsyntaxhighlight>
 
'''Emirps'''
<langsyntaxhighlight lang="jq">def is_emirp:
. as $n
| tostring | explode | reverse | implode | tonumber | (. != $n) and is_prime ;
Line 1,973 ⟶ 2,144:
else if ($p | is_emirp) then [.[0] + 1, $p] else .[1] = null end
end;
if .[1] then . else empty end ) ;</langsyntaxhighlight>
 
'''The tasks'''
 
(0) The three separate subtasks can be accomplished in one step as follows:
<langsyntaxhighlight lang="jq">emirps(10000)
| select( .[0] <= 20 or (7700 <= .[1] and .[1] <= 8000) or .[0] == 10000)</langsyntaxhighlight>
 
The output of the above is shown below.
Line 1,986 ⟶ 2,157:
 
(1) First twenty:
<syntaxhighlight lang ="jq">emirps(20)</langsyntaxhighlight>
 
(2) Selection by value
<syntaxhighlight lang="text">label $top
| primes
| if (7700 <= .) and (. <= 8000) and is_emirp then .
elif . > 8000 then break $top
else empty
end</langsyntaxhighlight>
 
(3) 10,000th
<syntaxhighlight lang="text">last(emirps(10000)) | .[1]</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="sh">$ jq -c -n -f Emirp_primes.jq
[1,13]
[2,17]
Line 2,032 ⟶ 2,203:
[189,7951]
[190,7963]
[10000,948349]</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function collapse(n::Array{<:Integer})
Line 2,065 ⟶ 2,236:
println("Between 7700 and 8000:\n", filter(x -> 7700 ≤ x ≤ 8000, emirps))
println("10000th:\n", emirps[10000])
</langsyntaxhighlight>{{out}}
<pre>
First 20:
Line 2,077 ⟶ 2,248:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="scala">// version 1.1.4
 
fun isPrime(n: Int) : Boolean {
Line 2,143 ⟶ 2,314:
while(true)
print(i)
}</langsyntaxhighlight>
 
{{out}}
Line 2,155 ⟶ 2,326:
The 10,000th Emirp prime is : 948349
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def prime
{def prime.r
{lambda {:m :n}
{if {> {* :m :m} :n}
then :n
else {if {= {% :n :m} 0}
then false
else {prime.r {+ :m 1} :n}}
}}}
{lambda {:n}
{prime.r 2 :n}
}}
-> prime
 
{def emirp
{lambda {:n}
{let { {:n :n}
{:p {prime :n}}
{:q {prime {W.reverse :n}}}
} {if {and {not {= :p :q}}
{not :p .}
{not :q .} }
then :n
else .}}}}
-> emirp
 
{def emirps
{def emirps.loop
{lambda {:n :m :a :i :j}
{if {or {>= :j :n} {> :i :m}}
then :a with :i tests
else {emirps.loop :n :m
{if {W.equal? {emirp :i} :i}
then {A.addlast! :i :a} {+ :i 2} {+ :j 1}
else :a {+ :i 2} :j}}
}}}
{lambda {:i :n :m}
{emirps.loop :n :m {A.new} :i 0}
}}
-> emirps
 
{emirps 13 20 500}
-> [13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389] with 391 tests
 
{emirps 7701 11 10000}
-> [7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963] with 7965 tests
 
{emirps 948300 10 1000000}
-> {emirps 948300 10 1000000} // stackoverflow
 
{emirp 948349}
-> 948349
 
</syntaxhighlight>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
function isPrime (n)
if n < 2 then return false end
Line 2,199 ⟶ 2,427:
print("Wrong number of arguments")
end
</syntaxhighlight>
</lang>
Command prompt session:
<pre>
Line 2,213 ⟶ 2,441:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">EmirpPrime := proc(n)
local eprime;
eprime := parse(StringTools:-Reverse(convert(n,string)));
Line 2,235 ⟶ 2,463:
EmirpPrime~([seq(7700..8000)]);
EmirpsList(10000)[-1];
</syntaxhighlight>
</lang>
{{out}}
<pre>[13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157, 167, 179, 199, 311, 337, 347, 359, 389]
Line 2,243 ⟶ 2,471:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
First a simple helper function
<langsyntaxhighlight Mathematicalang="mathematica">reverseDigits[n_Integer] := FromDigits@Reverse@IntegerDigits@n</langsyntaxhighlight>
A function to test whether n is an emirp prime
<langsyntaxhighlight Mathematicalang="mathematica">emirpQ[n_Integer] :=
Block[{rev = reverseDigits@n}, And[n != rev, PrimeQ[rev]]]</langsyntaxhighlight>
Note, this test function assumes n is prime. Adding a check to verify n is prime will have
an impact on execution time for finding the mth emirp prime particularly when m is large.
 
Finally, a function which returns the first emirp prime larger than the supplied argument
<langsyntaxhighlight Mathematicalang="mathematica">nextEmirp[n_Integer] :=
NestWhile[NextPrime, NextPrime[n], ! emirpQ[#] &]</langsyntaxhighlight>
 
With these the first 20 emirp primes are computed as:
<langsyntaxhighlight Mathematicalang="mathematica">Rest@NestList[nextEmirp, 1, 20]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,261 ⟶ 2,489:
 
The emirp primes betweewn 7700 and 8000 are:
<langsyntaxhighlight Mathematicalang="mathematica">Rest@NestWhileList[nextEmirp, 7700, # < 8000 &]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,267 ⟶ 2,495:
 
The 10,000th emirp prime is:
<syntaxhighlight lang Mathematica="mathematica">Nest[nextEmirp, 1, 10000]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,273 ⟶ 2,501:
 
=={{header|MATLAB}}==
<syntaxhighlight lang="matlab">
<lang Matlab>
NN=(1:1:1e6); %Natural numbers between 1 and t
pns=NN(isprime(NN)); %prime numbers
Line 2,279 ⟶ 2,507:
a=pns(isprime(p)); b=p(isprime(p)); c=a-b;
emirps=NN(a(c~=0));
</syntaxhighlight>
</lang>
{{out}}
<pre>the first twenty emirps are: emirps(1:20)
Line 2,311 ⟶ 2,539:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Emirp;
FROM Conversions IMPORT StrToLong;
FROM FormatString IMPORT FormatString;
Line 2,417 ⟶ 2,645:
 
ReadChar
END Emirp.</langsyntaxhighlight>
 
=={{header|Nim}}==
===Using a simple test of primality===
This is not the most efficient way to solve the tasks, but it doesn’t set a limit ''a priori''. We have done some optimizations to speed up the primality test. Using a cache didn’t improve the times. The program runs in about 100 ms.
<langsyntaxhighlight Nimlang="nim">import math
 
# Increments to find the next divisor when testing primality.
Line 2,492 ⟶ 2,720:
if count == 10000:
echo n
break</langsyntaxhighlight>
 
{{out}}
Line 2,502 ⟶ 2,730:
The sieve is a simple one, with no optimization to reduce its size. As it is a non-extensible sieve, there is a limit. So, in task 3, we check if the sieve size must be increased. The program runs in about 6 ms.
 
<langsyntaxhighlight Nimlang="nim">import math
 
const N = 1_000_000
Line 2,566 ⟶ 2,794:
break
if count < 10000:
echo "Not enough primes. Increase value of N."</langsyntaxhighlight>
 
{{out}}
Line 2,573 ⟶ 2,801:
The 10000th emirp: 948349</pre>
 
=={{header|OforthOCaml}}==
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_emirps =
let is_emirp n = let m = int_reverse n in m <> n && is_prime m in
seq_primes |> Seq.filter is_emirp
 
let () =
let seq_show sq = print_newline (Seq.iter (Printf.printf " %u") sq) in
seq_emirps |> Seq.take 20 |> seq_show;
seq_emirps |> Seq.drop_while ((>) 7700) |> Seq.take_while ((>) 8000) |> seq_show;
seq_emirps |> Seq.drop 9999 |> Seq.take 1 |> seq_show</syntaxhighlight>
{{out}}
<pre>
13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
948349
</pre>
 
=={{header|Oforth}}==
Using isPrime function of Primality by trial division task :
 
<langsyntaxhighlight Oforthlang="oforth">: isEmirp(n)
n isPrime ifFalse: [ false return ]
n asString reverse asInteger dup n == ifTrue: [ drop false ] else: [ isPrime ] ;
Line 2,588 ⟶ 2,842:
dup isEmirp ifTrue: [ dup l add ] 1 +
]
drop l ;</langsyntaxhighlight>
 
{{out}}
Line 2,603 ⟶ 2,857:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">rev(n)=subst(Polrev(digits(n)),'x,10);
emirp(n)=my(r=rev(n)); isprime(r) && isprime(n) && n!=r
select(emirp, primes(100))[1..20]
select(emirp, primes([7700,8000]))
s=10000; forprime(p=2,,if(emirp(p) && s--==0, return(p)))</langsyntaxhighlight>
{{out}}
<pre>%1 = [13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157, 167, 179, 199, 311, 337, 347, 359, 389]
Line 2,617 ⟶ 2,871:
using trial division unit , but jumping over number ranges, where the reversed numbers can't be a prime.
Compiles with Delphi and Free Pascal.
<langsyntaxhighlight lang="pascal">program Emirp;
//palindrome prime 13 <-> 31
{$IFDEF FPC}
Line 2,865 ⟶ 3,119:
OutPutHelp;
end;
End.</langsyntaxhighlight>
;output:
<pre>
Line 2,906 ⟶ 3,160:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use feature 'say';
use ntheory qw(forprimes is_prime);
 
Line 2,928 ⟶ 3,182:
forprimes { print " $_" if is_prime(reverse $_) && $_ ne reverse($_) } 7700,8000;
print "\n";
say "The 10_000'th emirp: ", (emirp_list(10000))[-1];</langsyntaxhighlight>
{{out}}
<pre>First 20: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Line 2,936 ⟶ 3,190:
=={{header|Phix}}==
Does not assume anywhere that some pre-guessed value will be enough.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">emirps</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 3,013 ⟶ 3,267:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cl</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}:</span><span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">())</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1-20"</span><span style="color: #0000FF;">)</span>
Line 3,023 ⟶ 3,277:
<span style="color: #000000;">usage</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{Out}}
<pre>
Line 3,033 ⟶ 3,287:
=={{header|PHP}}==
 
<langsyntaxhighlight PHPlang="php"><?php
 
function is_prime($n) {
Line 3,078 ⟶ 3,332:
'First twenty emirps :', PHP_EOL, $first20, PHP_EOL,
'Emirps between 7,700 and 8,000 :', PHP_EOL, $between, PHP_EOL,
'The 10,000th emirp :', PHP_EOL, $x, PHP_EOL;</langsyntaxhighlight>
 
{{out}}
Line 3,089 ⟶ 3,343:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de prime? (N)
(and
(bit? 1 N)
Line 3,124 ⟶ 3,378:
(println (take1 20))
(println (take2))
(println (take3))</langsyntaxhighlight>
{{out}}
<pre>(13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389)
Line 3,131 ⟶ 3,385:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process or(!);
pt1: Proc(run) Options(main);
/*********************************************************************
Line 3,294 ⟶ 3,548:
End;
 
End;</langsyntaxhighlight>
 
{{out}}
Line 3,314 ⟶ 3,568:
 
There is no explicit hard-coded ceiling added to the code for the prime generator, which is the reason given for the need to invoke a program three times in the task description.
<langsyntaxhighlight lang="python">from __future__ import print_function
from prime_decomposition import primes, is_prime
from heapq import *
Line 3,340 ⟶ 3,594:
if pr >= 7700: print(pr, end=', ')
print(']')
print('10000th:\n ', list(islice(emirp(), 10000-1, 10000)))</langsyntaxhighlight>
 
{{out}}
Line 3,354 ⟶ 3,608:
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> 1000000 eratosthenes
[ [] swap
Line 3,387 ⟶ 3,641:
[ dip 1+ ]
over 10000 = until ]
nip echo cr</langsyntaxhighlight>
 
{{out}}
Line 3,396 ⟶ 3,650:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
library(gmp)
 
Line 3,421 ⟶ 3,675:
cat("The 10000th emirp: ")
emirp(ignore = 9999, howmany = 1)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,440 ⟶ 3,694:
basic functions, unburdened by accounting or (too many) performance
considerations (please don't mark this as needing attention... I know it falls short of
<langsyntaxhighlight lang="racket">#lang racket
(require math/number-theory)
 
Line 3,460 ⟶ 3,714:
(let loop ((i 10000) (p 9))
(define p+2 (+ p 2))
(cond [(not (emirp-prime? p+2)) (loop i p+2)] [(= i 1) p+2] [else (loop (- i 1) p+2)]))</langsyntaxhighlight>
 
The second is somewhat larger and seems to be a playground for all sorts of code.
<langsyntaxhighlight lang="racket">#lang racket
;; ---------------------------------------------------------------------------------------------------
;; There are two distinct requirements here...
Line 3,607 ⟶ 3,861:
(check-equal? (time (task2 emirp-prime?/sieve)) (time (task2)))
(check-equal? (time (task3 emirp-prime?/sieve extend-sieve!)) (time (task3))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,636 ⟶ 3,890:
For better performance, build the lazy list using module <code>Math::Primesieve</code>, not the built-in, then display results based on parameters passed in. The default is to display an array slice starting and stopping at the given indices. Alternately, ask for all values between two endpoints.
 
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
 
sub prime-hash (Int $max) {
Line 3,661 ⟶ 3,915:
}
}
}</langsyntaxhighlight>
{{out}}
Run with passed parameters: 1 20
Line 3,681 ⟶ 3,935:
<br>memoization was added (assisting with the <big>√{{overline|&nbsp;j&nbsp;}}</big>), &nbsp; and some of the trial divisions were hard-coded to minimize
<br>the CPU time a bit.
<langsyntaxhighlight lang="rexx">/*REXX program finds emirp primes (base 10): when a prime reversed, is another prime.*/
parse arg x y . /*obtain optional arguments from the CL*/
if x=='' | x=="," then do; x=1; y=20; end /*Not specified? Then use the default.*/
Line 3,716 ⟶ 3,970:
/* [↓] display the emirp list. */
say strip($); say; n=words($); ?=(n\==1) /*display the emirp primes wanted. */
if ? then say n 'emirp primes shown.' /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; when using the following for input: &nbsp; <tt> 1 &nbsp; 20 </tt>
<pre>
Line 3,735 ⟶ 3,989:
 
===version 2===
<langsyntaxhighlight lang="rexx"> /*********************************************************************
* 27.03.2014 Walter Pachl
*********************************************************************/
Line 3,861 ⟶ 4,115:
Say l
End
Return</langsyntaxhighlight>
'''output'''<pre>rexx ptz 1
the first 20 emirps:
Line 3,884 ⟶ 4,138:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
nr = 1
m = 2
Line 3,933 ⟶ 4,187:
next
return true
</syntaxhighlight>
</lang>
 
=={{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]]. Requirement 3 of the task needs too much execution time to be allowed by the emulator watchdog timer.
{{works with|Halcyon Calc|4.2.7}}
≪ 0 SWAP
IF DUP R→B '''BPRIM?''' THEN
→STR DUP '''RVSTR'''
IF DUP ROT ≠ THEN STR→ R→B '''BPRIM?''' OR DUP END
END DROP
≫ ''''BMIRP?'''' STO
 
≪ { } 13 WHILE OVER SIZE 20 < REPEAT
IF DUP '''BMIRP?''' THEN DUP ROT SWAP + SWAP END
2 +
END DROP
{ } 7700 8000 FOR n
IF n '''BMIRP?''' THEN n + END
NEXT
≫ EVAL
{{out}}
<pre>
2: { 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389 }
1: { 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963 }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
emirp = Enumerator.new do |y|
Line 3,953 ⟶ 4,231:
break
end
end</langsyntaxhighlight>
 
{{out}}
Line 3,966 ⟶ 4,244:
 
=={{header|Rust}}==
<syntaxhighlight lang="text">#![feature(iterator_step_by)]
 
extern crate primal;
Line 4,015 ⟶ 4,293:
println!("Emirps-s between 7700 and 8000 : {:?}", vec2);
println!("10.000-th emirp : {}", emirp_10_000);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,030 ⟶ 4,308:
===Using BigInt's isProbablePrime()===
The isProbablePrime() method performs a Miller-Rabin primality test to within a given certainty.
<langsyntaxhighlight lang="scala">def isEmirp( v:Long ) : Boolean = {
val b = BigInt(v.toLong)
val r = BigInt(v.toString.reverse.toLong)
Line 4,042 ⟶ 4,320:
println( "%32s".format( "Emirps between %d and %d: ".format( b1, b2 )) + {for( i <- b1 to b2 if( isEmirp(i) ) ) yield i}.mkString(",") )
println( "%32s".format( "%,d emirp: ".format( c )) + Iterator.from(2).filter( isEmirp(_) ).drop(c-1).next )
}</langsyntaxhighlight>
{{out}}
<pre> First 20 emirps: 13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389
Emirps between 7700 and 8000: 7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963
10,000 emirp: 948349</pre>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<syntaxhighlight lang="scheme">; Primality test by simple trial division.
(define prime?
(lambda (num)
(if (< num 2)
#f
(let loop ((div 2))
(cond ((> (* div div) num) #t)
((zero? (modulo num div)) #f)
(else (loop (1+ div))))))))
 
; Check if number is an emirp prime.
(define emirp?
(lambda (num)
(and (prime? num)
(let ((rev (string->number (list->string (reverse (string->list (number->string num)))))))
(and (not (= num rev)) (prime? rev))))))
 
(printf "The first 20 emirps:")
(do ((num 1 (1+ num)) (cnt 0))
((>= cnt 20))
(when (emirp? num)
(set! cnt (1+ cnt))
(printf " ~d" num)))
(newline)
 
(printf "All emirps between 7700 and 8000:")
(do ((num 7700 (1+ num)))
((>= num 8000))
(when (emirp? num)
(printf " ~d" num)))
(newline)
 
(printf "The 10000th emirp: ~d~%"
(do ((num 1 (1+ num)) (cnt 0))
((>= cnt 10000) (1- num))
(when (emirp? num) (set! cnt (1+ cnt)))))</syntaxhighlight>
{{out}}
<pre>The first 20 emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
All emirps between 7700 and 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
The 10000th emirp: 948349</pre>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func forprimes(a, b, callback) {
for (var p = a.dec.next_prime; p <= b; p.next_prime!) {
callback(p)
Line 4,080 ⟶ 4,400:
forprimes(7700, 8000, {|p| is_emirp(p) && take(p) })
}.join(' '))
say ("The 10,000'th emirp: ", emirp_list(10000)[-1])</langsyntaxhighlight>
{{out}}
<pre>
Line 4,095 ⟶ 4,415:
 
First an emirp checker:
<langsyntaxhighlight lang="smalltalk">isEmirp :=
[:p | |e|
(e := p asString reversed asNumber) isPrime
and:[ e ~= p ]
].</langsyntaxhighlight>
 
an infinite list of primes:
<langsyntaxhighlight lang="smalltalk">primeGen :=
[:n |
LazyCons car:n cdr:[primeGen value:(n nextPrime)]
].</langsyntaxhighlight>
 
an infinite list of emirps, taking an infinite list of primes as arg:
<langsyntaxhighlight lang="smalltalk">emirpGen :=
[:l | |rest el|
rest := l.
[ el := rest car. rest := rest cdr. isEmirp value:el ] whileFalse.
LazyCons car:el cdr:[emirpGen value:rest]
].</langsyntaxhighlight>
two infinite lists:
<langsyntaxhighlight lang="smalltalk">listOfPrimes := primeGen value:2.
listOfEmirps := emirpGen value:listOfPrimes.</langsyntaxhighlight>
generating output:
<langsyntaxhighlight lang="smalltalk">Transcript
show:'first 20 emirps: ';
showCR:(listOfEmirps take:20) asArray.
Line 4,128 ⟶ 4,448:
Transcript
show:'10000''th emirp: ';
showCR:(listOfEmirps nth:10000).</langsyntaxhighlight>
 
Generates:<pre>
Line 4,136 ⟶ 4,456:
 
LazyCons is easily defined as:
<langsyntaxhighlight lang="smalltalk">Object subclass: #Cons
instancevariableNames:'car cdr'.
 
Line 4,153 ⟶ 4,473:
cdr := cdr value.
self changeClassTo:Cons.
^cdr</langsyntaxhighlight>
 
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">emirp 1000
list in 1/20, noobs noh
 
Line 4,210 ⟶ 4,530:
+--------+
| 948349 |
+--------+</langsyntaxhighlight>
 
Now the definition of ''emirp.ado'':
 
<langsyntaxhighlight lang="stata">program emirp
args n
qui clear
Line 4,246 ⟶ 4,566:
st_store(.,1,a)
}
end</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension BinaryInteger {
Line 4,295 ⟶ 4,615:
print("First 20 emirps: \(Array(lots.prefix(20)))")
print("Emirps between 7700 and 8000: \(rang)")
print("10,000th emirp: \(Array(lots).last!)")</langsyntaxhighlight>
 
{{out}}
Line 4,305 ⟶ 4,625:
=={{header|Tcl}}==
{{tcllib|math::numtheory}}
<langsyntaxhighlight lang="tcl">package require math::numtheory
 
# Import only to keep line lengths down
Line 4,328 ⟶ 4,648:
if {[emirp? $n] && [incr ne] == 10000} break
}
puts "10,000: $n"</langsyntaxhighlight>
{{out}}
<pre>
Line 4,338 ⟶ 4,658:
=={{header|VBA}}==
 
<langsyntaxhighlight lang="vb">Option Explicit
 
Private Const MAX As Long = 5000000
Line 4,451 ⟶ 4,771:
On Error GoTo 0
End If
End Function</langsyntaxhighlight>
{{out}}
<pre>At this point : Execution time = 13,23047 seconds.
Line 4,462 ⟶ 4,782:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
 
Module Module1
Line 4,544 ⟶ 4,864:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>First 20:
Line 4,557 ⟶ 4,877:
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
var isEmirp = Fn.new{ |n|
Line 4,596 ⟶ 4,916:
}
}
System.print(i)</langsyntaxhighlight>
 
{{out}}
Line 4,607 ⟶ 4,927:
 
The 10,000th emirp is 948349
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
func Reverse(N); \Return the value of N with its digits reversed
int N, M;
[M:= 0;
repeat N:= N/10;
M:= M*10 + rem(0);
until N = 0;
return M;
];
 
int N, M, Cnt;
[N:= 13; Cnt:= 0;
Text(0, "First 20 emirps:^m^j");
loop [if IsPrime(N) then
[M:= Reverse(N);
if IsPrime(M) and M # N then
[Cnt:= Cnt+1;
if Cnt <= 20 then
[IntOut(0, N); ChOut(0, ^ )];
if Cnt = 20 then
Text(0, "^m^jEmirps between 7700 and 8000:^m^j");
if N >= 7700 and N <= 8000 then
[IntOut(0, N); ChOut(0, ^ )];
if Cnt = 10_000 then
[Text(0, "^m^jThe 10,000 emirp: ");
IntOut(0, N);
CrLf(0);
quit;
];
];
];
N:= N+2;
];
]</syntaxhighlight>
{{out}}
<pre>
First 20 emirps:
13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
Emirps between 7700 and 8000:
7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
The 10,000 emirp: 948349
</pre>
 
=={{header|zkl}}==
Uses the solution from task [[Extensible prime generator#zkl]]. Saves the primes to a list, which gets pretty big.
<langsyntaxhighlight lang="zkl">var PS=Import("Src/ZenKinetic/sieve").postponed_sieve;
var ps=Utils.Generator(PS), plist=ps.walk(10).copy();
 
Line 4,625 ⟶ 4,999:
Utils.Generator(PS).filter(fcn(p){if(p>8000)return(Void.Stop); p>7700 and isEmirp(p)});
 
Utils.Generator(PS).reduce(fcn(N,p){N+=isEmirp(p); (N==10000) and T(Void.Stop,p) or N },0);</langsyntaxhighlight>
{{out}}
<pre>
2,044

edits