Erdős-primes: Difference between revisions

Add APL
m (→‎{{header|C#|CSharp}}: lol, ordinal, not cardinal...)
(Add APL)
 
(40 intermediate revisions by 15 users not shown)
Line 15:
:*   the OEIS entry:   [http://oeis.org/A064152 A064152 Erdos primes].
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
I is_prime[n]
L(i) (n * n .. limit).step(n)
is_prime[i] = 0B
R is_prime
 
V is_prime = primes_upto(1'000'000)
V primeList = enumerate(is_prime).filter((i, prime) -> prime).map((i, prime) -> i)
 
[Int] factorials
L(n) 1..
I factorial(n) >= 1'000'000
L.break
factorials.append(factorial(n))
 
F isErdosPrime(p)
L(f) :factorials
I f >= p
L.break
I :is_prime[p - f]
R 0B
R 1B
 
[Int] erdosList2500
L(p) primeList
I p >= 2500
L.break
I isErdosPrime(p)
erdosList2500.append(p)
 
print(‘Found ’erdosList2500.len‘ Erdos primes less than 2500:’)
L(prime) erdosList2500
print(‘#5’.format(prime), end' I (L.index + 1) % 10 == 0 {"\n"} E ‘ ’)
print()
 
V count = 0
L(p) primeList
I isErdosPrime(p)
count++
I count == 7875
print("\nThe 7875th Erdos prime is "p‘.’)
L.break</syntaxhighlight>
 
{{out}}
<pre>
Found 25 Erdos primes less than 2500:
2 101 211 367 409 419 461 557 673 709
769 937 967 1009 1201 1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
 
The 7875th Erdos prime is 999721.
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO REPORT prime n:
SELECT:
n < 2: FAIL
n mod 2 = 0: REPORT n=2
ELSE: REPORT NO d IN {2..floor (root n)} HAS n mod d = 0
 
HOW TO REPORT erdos p:
IF NOT prime p: FAIL
PUT 1, 1 IN k, k.fac
WHILE k.fac < p:
IF prime (p - k.fac): FAIL
PUT k+1 IN k
PUT k.fac*k IN k.fac
SUCCEED
 
PUT 0 IN nprimes
FOR n IN {1..2499}:
IF erdos n:
WRITE n>>6
PUT nprimes+1 IN nprimes
IF nprimes mod 10 = 0: WRITE/
 
WRITE /
WRITE "There are `nprimes` Erdos primes < 2500."/
 
PUT 2499 IN n
WHILE nprimes < 7875:
PUT n+2 IN n
IF erdos n: PUT nprimes + 1 IN nprimes
 
WRITE "The `nprimes`th Erdos prime is `n`."/</syntaxhighlight>
{{out}}
<pre> 2 101 211 367 409 419 461 557 673 709
769 937 967 1009 1201 1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
There are 25 Erdos primes < 2500.
The 7875th Erdos prime is 999721.</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsErdosPrime(INT x BYTE ARRAY primes)
INT k,f
 
IF primes(x)=0 THEN
RETURN (0)
FI
 
k=1 f=1
WHILE f<x
DO
IF primes(x-f) THEN
RETURN (0)
FI
k==+1
f==*k
OD
RETURN (1)
 
PROC Main()
DEFINE MAX="2499"
BYTE ARRAY primes(MAX+1)
INT i,count=[0]
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
FOR i=2 TO MAX
DO
IF IsErdosPrime(i,primes) THEN
PrintI(i) Put(32)
count==+1
FI
OD
PrintF("%E%EThere are %I Erdos primes",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Erd%C5%91s-primes.png Screenshot from Atari 8-bit computer]
<pre>
2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437
 
There are 25 Erdos primes
</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # find Erdős primes - primes p where p-k! is composite for all 1<=k!<p #
# returns TRUE if p is an Erdős prime #
PROC is erdos prime = ( INT p )BOOL:
IF NOT prime[ p ]
THEN FALSE
ELSE
BOOL result := TRUE;
FOR k WHILE factorial[ k ] < p AND result DO
result := NOT prime[ p - factorial[ k ] ]
OD;
result
FI # is erdos prime # ;
INT max prime = 1 000 000; # maximum number we will consider #
INT max erdos = 7 875; # maximum Erdős prime to find #
# construct a table of factorials large enough for max prime #
[ 1 : 12 ]INT factorial;
factorial[ 1 ] := 1;
FOR f FROM 2 TO UPB factorial DO
factorial[ f ] := factorial[ f - 1 ] * f
OD;
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL prime = PRIMESIEVE max prime; # sieve the primes to max prime #
INT max show = 2 500;
# find the Erdős primes, showing the ones up to max show #
INT e count := 0;
IF is erdos prime( 2 ) THEN
print( ( " 2" ) );
e count +:= 1
FI;
INT last erdos := 0;
FOR p FROM 3 BY 2 TO max show DO
IF is erdos prime( p ) THEN
print( ( " ", whole( p, 0 ) ) );
last erdos := p;
e count +:= 1
FI
OD;
print( ( newline, "Found ", whole( e count, 0 )
, " Erdos primes up to ", whole( max show, 0 ), newline ) );
# find the max erdos'th Erdős prime #
FOR p FROM max show WHILE e count < max erdos DO
IF is erdos prime( p ) THEN
last erdos := p;
e count +:= 1
FI
OD;
print( ( whole( last erdos, 0 ), " is Erdos prime ", whole( e count, 0 ), newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437
Found 25 Erdos primes up to 2500
999721 is Erdos prime 7875
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">erdos_primes←{
prime ← {(⍵≥2) ∧ 0∧.≠(1↓⍳⌊⍵*÷2)|⍵}
erdos ← {(prime ⍵) ∧ ∧/~prime¨ ⍵-!⍳⌊(!⍣¯1)⍵}
e2500 ← (erdos¨e)/e←⍳2500
⎕←e2500
⎕←'There are ',(⍕⍴e2500),' Erdős numbers ≤ 2500'
}</syntaxhighlight>
{{out}}
<pre>2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437
There are 25 Erdős numbers ≤ 2500</pre>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">factorials: map 1..20 => [product 1..&]
erdos?: function [x][
if not? prime? x -> return false
Line 30 ⟶ 243:
 
loop split.every:10 select 2..2500 => erdos? 'a ->
print map a => [pad to :string & 5]</langsyntaxhighlight>
 
{{out}}
Line 39 ⟶ 252:
 
=={{header|AWK}}==
{{trans|FreeBASIC}}
<lang AWK>
<syntaxhighlight lang="awk">
# syntax: GAWK -f ERDOS-PRIMES.AWK
# converted from FreeBASIC
Line 77 ⟶ 291:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 85 ⟶ 299:
</pre>
 
=={{header|C#|CSharpBASIC}}==
==={{header|FreeBASIC}}===
<lang csharp>using System; using static System.Console;
I won't bother reproducing a primality-testing function; use the one from [[Primality_by_trial_division#FreeBASIC]].
<syntaxhighlight lang="freebasic">#include "isprime.bas"
 
function is_erdos_prime( p as uinteger ) as boolean
if not isprime(p) then return false
dim as uinteger kf=1, m=1
while kf < p
kf*=m : m+=1
if isprime(p - kf) then return false
wend
return true
end function
 
dim as integer c = 0, i = 1
while c<7875
i+=1
if is_erdos_prime(i) then
c+=1
if i<2500 or c=7875 then print c, i
end if
wend</syntaxhighlight>
{{out}}<pre>1 2
2 101
3 211
4 367
5 409
6 419
7 461
8 557
9 673
10 709
11 769
12 937
13 967
14 1009
15 1201
16 1259
17 1709
18 1831
19 1889
20 2141
21 2221
22 2309
23 2351
24 2411
25 2437
7875 999721
</pre>
 
==={{header|Palo Alto Tiny BASIC}}===
{{trans|Tiny BASIC|Removed overlapping loops.}}
Without the stretch goal because numbers are limited to signed 16-bit integers.
<syntaxhighlight lang="basic">
10 REM ERDOS-PRIMES
20 LET P=2,C=1
30 PRINT C," ",P
40 FOR P=3 TO 2500 STEP 2
50 LET Z=P;GOSUB 1000
60 IF A=0 GOTO 160
70 REM F = K!
80 LET K=1,F=1,Z=P-F
90 IF Z<0 GOTO 150
100 GOSUB 1000
110 IF A=1 GOTO 150
120 LET K=K+1,F=F*K,Z=P-F
130 IF Z<0 GOTO 150
140 GOTO 100
150 IF Z<0 LET C=C+1;PRINT C," ",P
160 NEXT P
170 STOP
980 REM PRIMALITY OF Z BY TRIAL DIVISION
990 REM RESULT IS IN A
1000 LET A=0
1010 IF Z=2 LET A=1;RETURN
1020 IF Z<3 RETURN
1030 LET Y=2
1040 IF (Z/Y)*Y=Z RETURN
1050 IF Y*Y>=Z LET A=1;RETURN
1060 LET Y=Y+1
1070 GOTO 1040
1080 RETURN
</syntaxhighlight>
{{out}}
<pre>
1 2
2 101
3 211
4 367
5 409
6 419
7 461
8 557
9 673
10 709
11 769
12 937
13 967
14 1009
15 1201
16 1259
17 1709
18 1831
19 1889
20 2141
21 2221
22 2309
23 2351
24 2411
25 2437
</pre>
 
==={{header|Tiny BASIC}}===
Can't manage the stretch goal because integers are limited to signed 16 bit.
{{works with|TinyBasic}}
Tiny BASICs other than Tom Pittman's TinyBasic use <code>,</code> instead of <code>;</code> for string concatenation in <code>PRINT</code>.
<syntaxhighlight lang="basic">10 REM Erdős-primes
20 LET P = 1
30 LET C = 0
40 IF P > 2 THEN LET P = P + 2
50 IF P < 3 THEN LET P = P + 1
60 LET Z = P
70 GOSUB 1000
80 IF A = 0 THEN GOTO 40
90 LET K = 0
100 LET F = 1
110 LET K = K + 1
120 REM F = K!
130 LET F = F * K
140 LET Z = P - F
150 IF Z < 0 THEN GOTO 190
160 GOSUB 1000
170 IF A = 1 THEN GOTO 40
180 GOTO 110
190 LET C = C + 1
200 IF P < 2500 THEN PRINT C; " "; P
210 IF P > 2500 THEN END
220 GOTO 40
990 REM primality of Z by trial division, result is in A
1000 LET Y = 1
1010 LET A = 0
1020 IF Z = 2 THEN LET A = 1
1030 IF Z < 3 THEN RETURN
1040 LET Y = Y + 1
1050 IF (Z / Y) * Y = Z THEN RETURN
1060 IF Y * Y < Z THEN GOTO 1040
1070 LET A = 1
1080 RETURN</syntaxhighlight>
{{out}}
<pre>1 2
2 101
3 211
4 367
5 409
6 419
7 461
8 557
9 673
10 709
11 769
12 937
13 967
14 1009
15 1201
16 1259
17 1709
18 1831
19 1889
20 2141
21 2221
22 2309
23 2351
24 2411
25 2437
</pre>
 
=={{header|C}}==
{{trans|Wren}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
 
#define LIMIT 1000000
#define LOWER_LIMIT 2500
 
bool *sieve(int limit) {
int i, p;
limit++;
// True denotes composite, false denotes prime.
bool *c = calloc(limit, sizeof(bool)); // all false by default
c[0] = true;
c[1] = true;
for (i = 4; i < limit; i += 2) c[i] = true;
p = 3; // Start from 3.
while (true) {
int p2 = p * p;
if (p2 >= limit) break;
for (i = p2; i < limit; i += 2 * p) c[i] = true;
while (true) {
p += 2;
if (!c[p]) break;
}
}
return c;
}
 
int main() {
int i, j, fact, ec = 0, ec2 = 0, lastErdos = 0;
bool found;
bool *c = sieve(LIMIT-1);
int erdos[30];
for (i = 2; i < LIMIT;) {
if (!c[i]) {
j = 1;
fact = 1;
found = true;
while (fact < i) {
if (!c[i-fact]) {
found = false;
break;
}
++j;
fact *= j;
}
if (found) {
if (i < LOWER_LIMIT) erdos[ec2++] = i;
lastErdos = i;
++ec;
}
}
i = (i > 2) ? i + 2 : i + 1;
}
setlocale(LC_NUMERIC, "");
printf("The %'d Erdős primes under %'d are:\n", ec2, LOWER_LIMIT);
for (i = 0; i < ec2; ++i) {
printf("%6d ", erdos[i]);
if (!((i+1)%10)) printf("\n");
}
printf("\n\nThe %'dth Erdős prime is %'d.\n", ec, lastErdos);
free(c);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
The 25 Erdős primes under 2,500 are:
2 101 211 367 409 419 461 557 673 709
769 937 967 1009 1201 1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
 
The 7,875th Erdős prime is 999,721.
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System; using static System.Console;
class Program {
const int lmt = (int)1e6, first = 2500; static int[] f = new int[10];
Line 108 ⟶ 577:
if (x < 4) return x > 1; if ((x & 1) == 0) return false;
for (int i = 3; i * i <= x; i += 2) if (x % i == 0) return false;
return true; } } }</langsyntaxhighlight>
{{out}}
<pre> 2 101 211 367 409
Line 121 ⟶ 590:
=={{header|C++}}==
{{libheader|Primesieve}}
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iomanip>
#include <iostream>
Line 170 ⟶ 639:
std::wcout << L"\n\nThe " << max_count << L"th Erd\x151s prime is " << p << L".\n";
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 180 ⟶ 649:
 
The 7,875th Erdős prime is 999,721.
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Executes in 225 ms. It could be faster with a Factorial lookup table.
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: integer): boolean;
{Optimised prime test - about 40% faster than the naive approach}
var I,Stop: integer;
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));
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 Factorial(N: Word): int64;
var I: integer;
begin
Result:= 1;
for I := 2 to N do Result:=Result * I;
end;
 
 
function IsErdosPrime(P: integer): boolean;
{Test if specified Primes is Erdos}
{i.e. all p-k! for 1<=k!<p are composite.}
var K: integer;
var F: int64;
begin
K:=1;
Result:=False;
while True do
begin
F:=Factorial(K);
if F>=P then break;
if IsPrime(P-F) then exit;
Inc(K);
end;
Result:=True;
end;
 
 
procedure FindErdosPrimes(Memo: TMemo);
{Collect and display Erdos primes}
var P,I,Cnt: integer;
var Erdos: array of integer;
var S: string;
begin
{Collect all Erdos Primes<1,000,000}
for P:=2 to 1000000 do
if IsPrime(P) then
if IsErdosPrime(P) then
begin
SetLength(Erdos,Length(Erdos)+1);
Erdos[High(Erdos)]:=P;
end;
{Display the data in several different ways}
Memo.Lines.Add('25 Erdos primes less than 2500');
S:='';
for I:=0 to 24 do
begin
S:=S+Format('%8d',[Erdos[I]]);
if (((I+1) mod 5)=0) or (I=24) then
begin
Memo.Lines.Add(S);
S:='';
end;
end;
Memo.Lines.Add('Summary:');
Memo.Lines.Add('Number of Erdos Primes < 1-million: '+IntToStr(Length(Erdos)));
Memo.Lines.Add('Last Erdos Prime < 1-million: '+IntToStr(Erdos[High(Erdos)]));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
25 Erdos primes less than 2500
2 101 211 367 409
419 461 557 673 709
769 937 967 1009 1201
1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
Summary:
Number of Erdos Primes < 1-million: 7875
Last Erdos Prime < 1-million: 999721
 
</pre>
 
 
=={{header|EasyLang}}==
{{trans|Action!}}
<syntaxhighlight>
fastfunc isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func iserdosprim p .
if isprim p = 0
return 0
.
k = 1
f = 1
while f < p
if isprim (p - f) = 1
return 0
.
k += 1
f *= k
.
return 1
.
for p = 2 to 2499
if iserdosprim p = 1
write p & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437
</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">
// Erdős Primes. Nigel Galloway: March 22nd., 2021
let rec fN g=function 1->g |n->fN(g*n)(n-1)
Line 190 ⟶ 804:
let eP()=primes32()|>Seq.filter(fG 1>>Seq.forall id)
eP()|>Seq.takeWhile((>)2500)|>Seq.iter(printf "%d "); printfn "\n\n7875th Erdős prime is %d" (eP()|>Seq.item 7874)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 197 ⟶ 811:
7875th Erdos prime is 999721
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<syntaxhighlight lang="text">USING: formatting io kernel lists lists.lazy math
math.factorials math.primes math.primes.lists math.vectors
prettyprint sequences tools.memory.private ;
Line 216 ⟶ 831:
 
7874 erdős lnth commas
"The 7,875th Erdős prime is %s.\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 227 ⟶ 842:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 278 ⟶ 893:
 
2500 print_erdos_primes
bye</langsyntaxhighlight>
 
{{out}}
Line 288 ⟶ 903:
Count: 25
</pre>
 
=={{header|FreeBASIC}}==
I won't bother reproducing a primality-testing function; use the one from [[Primality_by_trial_division#FreeBASIC]].
<lang freebasic>#include "isprime.bas"
 
function is_erdos_prime( p as uinteger ) as boolean
if not isprime(p) then return false
dim as uinteger kf=1, m=1
while kf < p
kf*=m : m+=1
if isprime(p - kf) then return false
wend
return true
end function
 
dim as integer c = 0, i = 1
while c<7875
i+=1
if is_erdos_prime(i) then
c+=1
if i<2500 or c=7875 then print c, i
end if
wend</lang>
{{out}}<pre>1 2
2 101
3 211
4 367
5 409
6 419
7 461
8 557
9 673
10 709
11 769
12 937
13 967
14 1009
15 1201
16 1259
17 1709
18 1831
19 1889
20 2141
21 2221
22 2309
23 2351
24 2411
25 2437
7875 999721
</pre>
 
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main
 
import "fmt"(
"fmt"
 
"rcu"
func sieve(limit int) []bool {
)
limit++
// True denotes composite, false denotes prime.
c := make([]bool, limit) // all false by default
c[0] = true
c[1] = true
for i := 4; i < limit; i += 2 {
c[i] = true
}
p := 3 // Start from 3.
for {
p2 := p * p
if p2 >= limit {
break
}
for i := p2; i < limit; i += 2 * p {
c[i] = true
}
for {
p += 2
if !c[p] {
break
}
}
}
return c
}
 
func commatize(n int) string {
s := fmt.Sprintf("%d", n)
if n < 0 {
s = s[1:]
}
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
if n >= 0 {
return s
}
return "-" + s
}
 
func main() {
limit := int(1e6)
clowerLimit := sieve(limit - 1)2500
c := rcu.PrimeSieve(limit-1, true)
var erdos []int
lastErdos := 0
ec := 0
for i := 2; i < limit; {
if !c[i] {
Line 405 ⟶ 933:
}
if found {
erdosif =i append(erdos,< i)lowerLimit {
erdos = append(erdos, i)
}
lastErdos = i
ec++
}
}
Line 414 ⟶ 946:
}
}
fmt.Printf("The %d Erdős primes under %s are\n", len(erdos), rcu.Commatize(lowerLimit))
lowerLimit := 2500
rcu.PrintTable(erdos, 10, 6, false)
var erdosLower []int
fmt.Printf("\nThe %s Erdős prime is %s.\n", rcu.Commatize(ec), rcu.Commatize(lastErdos))
for _, e := range erdos {
}</syntaxhighlight>
if e < lowerLimit {
erdosLower = append(erdosLower, e)
} else {
break
}
}
fmt.Printf("The %d Erdős primes under %s are\n", len(erdosLower), commatize(lowerLimit))
for i, e := range erdosLower {
fmt.Printf("%6d", e)
if (i+1)%10 == 0 {
fmt.Println()
}
}
show := 7875
fmt.Printf("\n\nThe %s Erdős prime is %s.\n", commatize(show), commatize(erdos[show-1]))
}</lang>
 
{{out}}
<pre>
The 25 Erdős primes under 2,500 are
2 101 211 367 409 419 461 557 673 709
769 937 967 1009 1201 1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
 
The 7,875 Erdős prime is 999,721.
</pre>
 
=={{header|J}}==
Implementation:
 
<syntaxhighlight lang="j">NB. erdos primes greater than !k and less than or equal to !k+1 (where !k is the factorial of k)
erdospr=: {{ k=. y
f=. !k+0 1
p=. (#~ 1= f&I.) p:(+i.)/0 1+p:inv f
p#~*/|:0=1 p:p-/!i.1+k
}}
 
NB. erdos primes less than j
erdosprs=: {{ (#~ j&>);erdospr&.>i.>.!inv j=. y }}</syntaxhighlight>
 
Task examples:
 
<syntaxhighlight lang="j"> erdosprs 2500
2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437
(#,{:) erdosprs 1e6
7875 999721</syntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class ErdosPrimes {
Line 498 ⟶ 1,035:
return sieve;
}
}</langsyntaxhighlight>
 
{{out}}
Line 509 ⟶ 1,046:
The 7875th Erdős prime is 999721.
</pre>
 
 
=={{header|jq}}==
Line 517 ⟶ 1,053:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
 
def is_prime:
Line 534 ⟶ 1,070:
| . * . > $n
end;
</syntaxhighlight>
</lang>
'''Erdős-primes'''
<langsyntaxhighlight lang="jq">
def is_Erdos:
. as $p
Line 551 ⟶ 1,087:
# emit the Erdos primes
def Erdos: range(2; infinite) | select(is_Erdos);
</syntaxhighlight>
</lang>
'''The tasks'''
<langsyntaxhighlight lang="jq">"The Erdős primes less than 2500 are:", emit_until(. >= 2500; Erdos),
 
"\nThe 7875th Erdős prime is \(nth(7874; Erdos))."
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 590 ⟶ 1,126:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Formatting
 
function isErdős(p::Integer)
Line 607 ⟶ 1,143:
println(length(E2500), " Erdős primes < 2500: ", E2500)
println("The 7875th Erdős prime is ", format(Erdőslist[7875], commas=true))
</langsyntaxhighlight>{{out}}
<pre>
25 Erdős primes < 2500: [2, 101, 211, 367, 409, 419, 461, 557, 673, 709, 769, 937, 967, 1009, 1201, 1259, 1709, 1831, 1889, 2141, 2221, 2309, 2351, 2411, 2437]
The 7875th Erdős prime is 999,721
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function isPrime (x)
if x < 2 then return false end
if x < 4 then return true end
if x % 2 == 0 then return false end
for d = 3, math.sqrt(x), 2 do
if x % d == 0 then return false end
end
return true
end
 
function primes ()
local x, maxInt = 3, 2^53
local function yieldPrimes ()
coroutine.yield(2)
repeat
if isPrime(x) then coroutine.yield(x) end
x = x + 2
until x == maxInt
end
return coroutine.wrap(function() yieldPrimes() end)
end
 
function factorial (n)
local f = 1
for i = 2, n do
f = f * i
end
return f
end
 
function isErdos (p)
local k, factK = 1
repeat
factK = factorial(k)
if isPrime(p - factK) then return false end
k = k + 1
until factK >= p
return true
end
 
local nextPrime, count, prime = primes(), 0
while count < 7875 do
prime = nextPrime()
if isErdos(prime) then
if prime < 2500 then io.write(prime .. " ") end
count = count + 1
end
end
print("\n\nThe 7875th Erdos prime is " .. prime)</syntaxhighlight>
{{out}}
<pre>2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437
 
The 7875th Erdos prime is 999721</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[ErdosPrimeQ]
ErdosPrimeQ[p_Integer] := Module[{k},
If[PrimeQ[p],
k = 1;
While[k! < p,
If[PrimeQ[p - k!], Return[False]];
k++;
];
True
,
False
]
]
sel = Select[Range[2500], ErdosPrimeQ]
Length[sel]
sel = Select[Range[999999], ErdosPrimeQ];
{Length[sel], Last[sel]}</syntaxhighlight>
{{out}}
<pre>{2, 101, 211, 367, 409, 419, 461, 557, 673, 709, 769, 937, 967, 1009, 1201, 1259, 1709, 1831, 1889, 2141, 2221, 2309, 2351, 2411, 2437}
25
{7875, 999721}</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map show erdos2500)),
Stdout ("There are " ++ show (#erdos2500) ++ " Erdos numbers <2500\n")]
where erdos2500 = filter erdos [1..2499]
 
erdos :: num->bool
erdos p = prime p & ~or [prime (p-k) | k <- takewhile (<p) (scan (*) 1 [2..])]
 
prime :: num->bool
prime n = n=2 \/ n=3, if n<=4
prime n = False, if n mod 2=0
prime n = and [n mod d ~= 0 | d <- [2..entier (sqrt n)]]</syntaxhighlight>
{{out}}
<pre>2
101
211
367
409
419
461
557
673
709
769
937
967
1009
1201
1259
1709
1831
1889
2141
2221
2309
2351
2411
2437
There are 25 Erdos numbers <2500</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, sets, strutils, sugar
 
const N = 1_000_000
Line 665 ⟶ 1,320:
erdös7875 = p
break
echo "\nThe 7875th Erdös prime is $#.".format(erdös7875)</langsyntaxhighlight>
 
{{out}}
Line 677 ⟶ 1,332:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 697 ⟶ 1,352:
say 'Erdős primes < ' . (my $max = 2500) . ':';
say join ' ', before { $_ > 2500 } @Erdős;
say "\nErdős prime #" . @Erdős . ' is ' . $Erdős[-1];</langsyntaxhighlight>
{{out}}
<pre>Erdős primes < 2500:
Line 705 ⟶ 1,360:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">facts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
Line 725 ⟶ 1,380:
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</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: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %,d%s Erdos prime is %,d (%s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[$],</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 732 ⟶ 1,387:
 
The 7,875th Erdos prime is 999,721 (1.2s)
</pre>
 
=={{header|PL/0}}==
Without stretch goal.
<syntaxhighlight lang="pascal">
var p, c, z, k, isprime, factk, iskchecked;
 
procedure checkprimality;
var i, isichecked;
begin
isprime := 0;
if z = 2 then isprime := 1;
if z >= 3 then
begin
i := 2; isichecked := 0;
while isichecked = 0 do
begin
if (z / i) * i = z then isichecked := 1;
if isichecked = 0 then
if i * i >= z then
begin
isprime := 1; isichecked := 1
end;
i := i + 1
end
end
end;
 
begin
p := 2; c := 1;
! p;
p := 3;
while p <= 2500 do
begin
z := p; call checkprimality;
if isprime = 1 then
begin
k := 1; factk := 1; z := p - factk;
iskchecked := 1;
if z >= 0 then iskchecked := 0;
while iskchecked = 0 do
begin
call checkprimality;
if isprime = 1 then iskchecked := 1;
if isprime <> 1 then
begin
k := k + 1; factk := factk * k; z := p - factk;
iskchecked := 1;
if z >= 0 then iskchecked := 0
end
end;
if z < 0 then
begin
c := c + 1;
! p
end
end;
p := p + 2
end
end.
</syntaxhighlight>
{{out}}
<pre>
2
101
211
367
409
419
461
557
673
709
769
937
967
1009
1201
1259
1709
1831
1889
2141
2221
2309
2351
2411
2437
</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
Basic task only as PL/M integers are 8/16 bit unsigned.
<syntaxhighlight lang="plm">
100H: /* FIND ERDOS PRIMES: PRIMES P WHERE P-K! IS COMPOSITE FOR ALL 1<=K!<P */
 
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
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;
 
/* TASK */
 
DECLARE MAX$NUMBER LITERALLY '2500'
, FALSE LITERALLY '0'
, TRUE LITERALLY '0FFH'
;
 
DECLARE PRIME ( MAX$NUMBER )BYTE; /* SIEVE THE PRIMES TO MAX$NUMBER - 1 */
DECLARE I ADDRESS;
PRIME( 0 ), PRIME( 1 ) = FALSE;
PRIME( 2 ) = TRUE;
DO I = 3 TO LAST( PRIME ) BY 2; PRIME( I ) = TRUE; END;
DO I = 4 TO LAST( PRIME ) BY 2; PRIME( I ) = FALSE; END;
DO I = 3 TO LAST( PRIME ) BY 2;
IF PRIME( I ) THEN DO;
DECLARE S ADDRESS;
DO S = I + I TO LAST( PRIME ) BY I;
PRIME( S ) = FALSE;
END;
END;
END;
 
/* TABLE OF FACTORIALS */
DECLARE FACTORIAL ( 8 )ADDRESS INITIAL( 1, 1, 2, 6, 24, 120, 720, 5040 );
 
/* RETURNS TRUE IF P IS AN ERDOS PRIME, FALSE OTHERWISE */
IS$ERDOS$PRIME: PROCEDURE( P )BYTE;
DECLARE P ADDRESS;
DECLARE RESULT BYTE;
RESULT = PRIME( P );
IF RESULT THEN DO;
DECLARE K BYTE;
K = 1;
DO WHILE FACTORIAL( K ) < P AND RESULT;
RESULT = NOT PRIME( P - FACTORIAL( K ) );
K = K + 1;
END;
END;
RETURN RESULT;
END IS$ERDOS$PRIME ;
 
/* FIND THE ERDOS PRIMES */
DECLARE ( P, COUNT ) ADDRESS;
COUNT = 0;
IF IS$ERDOS$PRIME( 2 ) THEN DO;
COUNT = COUNT + 1;
CALL PR$STRING( .' 2$' );
END;
P = 1;
DO WHILE COUNT < 25;
P = P + 2;
IF IS$ERDOS$PRIME( P ) THEN DO;
COUNT = COUNT + 1;
CALL PR$CHAR( ' ' );
IF P < 1000 THEN CALL PR$CHAR( ' ' );
CALL PR$NUMBER( P );
IF COUNT MOD 5 = 0 THEN CALL PR$NL;
END;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
2 101 211 367 409
419 461 557 673 709
769 937 967 1009 1201
1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
</pre>
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my @factorial = 1, |[\*] 1..*;
Line 743 ⟶ 1,581:
put 'Erdős primes < 2500:';
put @Erdős[^(@Erdős.first: * > 2500, :k)]».&comma;
put "\nThe 7,875th Erdős prime is: " ~ @Erdős[7874].&comma;</langsyntaxhighlight>
{{out}}
<pre>Erdős primes < 2500:
Line 749 ⟶ 1,587:
 
The 7,875th Erdős prime is: 999,721</pre>
 
Alternately, using fewer hard coded values:
 
 
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers;
use List::Divvy;
 
my @factorial = 1, |[\*] 1..*;
my @Erdős = ^∞ .grep: { .is-prime and none($_ «-« @factorial.&upto: $_).is-prime }
 
put "Erdős primes < 2500:\n" ~ @Erdős.&before(2500)».&comma.batch(8)».fmt("%5s").join: "\n";
put "\nThe largest Erdős prime less than {comma 1e6.Int} is {comma .[*-1]} in {.&ordinal-digit} position."
given @Erdős.&before(1e6);</syntaxhighlight>
{{out}}
<pre>Erdős primes < 2500:
2 101 211 367 409 419 461 557
673 709 769 937 967 1,009 1,201 1,259
1,709 1,831 1,889 2,141 2,221 2,309 2,351 2,411
2,437
 
The largest Erdős prime less than 1,000,000 is 999,721 in 7875th position.</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program counts/displays the number of Erdos primes under a specified number N. */
parse arg n cols . /*get optional number of primes to find*/
if n=='' | n=="," then n= 2500 /*Not specified? Then assume default.*/
Line 801 ⟶ 1,660:
end /*k*/ /* [↑] only divide up to √ J */
#= # + 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 819 ⟶ 1,678:
 
999,721 is the 7,875th Erdos prime.
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlibcore.ring"
see "working..." + nl
row = 0
limit = 2500
 
for p = 1 to limit
flag = 1
if isprime(p)
for k = 1 to p
if factorial(k) < p
temp = p - factorial(k)
if not isprime(temp)
flag = 1
else
flag = 0
exit
ok
else
exit
ok
next
else
flag = 0
ok
if flag = 1
row++
see "" + p + " "
if row % 5 = 0
see nl
ok
ok
next
 
see nl + "Found " + row + " Erdos primes less than 2500" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
working...
2 101 211 367 409
419 461 557 673 709
769 937 967 1009 1201
1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
 
Found 25 Erdos primes less than 2500
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« 0 → p k
« 1 SF
'''WHILE''' p 'k' INCR FACT - DUP 0 > 1 FS? AND '''REPEAT'''
'''IF''' ISPRIME? '''THEN''' 1 CF '''END'''
'''END'''
DROP 1 FS?
» » '<span style="color:blue">ERDOS?</span>' STO <span style="color:grey">@ ( n → erdös(n) )</span>
« { } 2
'''WHILE''' DUP 2500 < '''REPEAT'''
'''IF''' DUP <span style="color:blue">ERDOS?</span> '''THEN''' SWAP OVER + SWAP '''END'''
NEXTPRIME
'''END'''
DROP DUP SIZE "count" →TAG
» '<span style="color:blue">TASK</span>' STO <span style="color:grey">@ ( → results )</span>
 
{{out}}
<pre>
2: { 2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437 }
1: count: 25.
</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 863 ⟶ 1,797:
println!("\nThe 7875th Erd\u{151}s prime is {}.", p);
}
}</langsyntaxhighlight>
 
{{out}}
Line 875 ⟶ 1,809:
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program erdos_primes;
loop for e in [1..2499] | erdos e do
nprint(lpad(str e, 6));
if (n +:= 1) mod 10=0 then print; end if;
end loop;
 
print;
print("There are " + str n + " Erdos numbers < 2500");
 
e := 2499;
loop while n < 7875 do
loop until erdos e do
e +:= 2;
end loop;
n +:= 1;
end loop;
 
print("The " + str n + "th Erdos number is " + str e);
 
op erdos(p);
return prime p and not exists k in faclist p | prime (p-k);
end erdos;
 
op faclist(n);
f := 1;
return [[i+:=1, f*:=i](2) : until n<f](..i-1);
end op;
 
op prime(n);
if n<=4 then
return n in [2,3];
end if;
return odd n and not exists d in [3, 5..floor (sqrt n)] | n mod d=0;
end op;
end program;</syntaxhighlight>
{{out}}
<pre> 2 101 211 367 409 419 461 557 673 709
769 937 967 1009 1201 1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
There are 25 Erdos numbers < 2500
The 7875th Erdos number is 999721</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_erdos_prime(p) {
 
return true if p==2
Line 892 ⟶ 1,868:
 
say ("Erdős primes <= 2500: ", 1..2500 -> grep(is_erdos_prime))
say ("The 7875th Erdős prime is: ", is_erdos_prime.nth(7875))</langsyntaxhighlight>
{{out}}
<pre>
Erdős primes <= 2500: [2, 101, 211, 367, 409, 419, 461, 557, 673, 709, 769, 937, 967, 1009, 1201, 1259, 1709, 1831, 1889, 2141, 2221, 2309, 2351, 2411, 2437]
The 7875th Erdős prime is: 999721
</pre>
 
=={{header|Tiny BASIC}}==
Can't manage the stretch goal because integers are limited to signed 16 bit.
 
<lang tinybasic> LET P = 1
10 IF P > 2 THEN LET P = P + 2
IF P < 3 THEN LET P = P + 1
LET Z = P
GOSUB 1000
IF A = 0 THEN GOTO 10
LET K = 0
20 LET K = K + 1
GOSUB 2000
LET Z = P - F
IF Z < 0 THEN GOTO 30
GOSUB 1000
IF A = 1 THEN LET E = 0
IF A = 1 THEN GOTO 10
GOTO 20
30 LET C = C + 1
IF P < 2500 THEN PRINT C," ",P
IF P > 2500 THEN END
GOTO 10
1000 REM primality of Z by trial division, result is in A
LET Y = 1
LET A = 0
IF Z = 2 THEN LET A = 1
IF Z < 3 THEN RETURN
1010 LET Y = Y + 2
IF (Z/Y)*Y = Z THEN RETURN
IF Y*Y < Z THEN GOTO 1010
LET A = 1
RETURN
2000 REM factorial of K, result is in F
LET A = 1
LET F = 1
2010 LET F = F*A
IF A=K THEN RETURN
LET A = A + 1
GOTO 2010</lang>
{{out}}<pre>1 2
2 101
3 211
4 367
5 409
6 419
7 461
8 673
9 709
10 769
11 937
12 967
13 1009
14 1201
15 1259
16 1709
17 1831
18 2141
19 2221
20 2351
21 2411
22 2437
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var limit = 1e6
var lowerLimit = 2500
var primes = Int.primeSieve(limit - 1, true)
var c = Int.primeSieve(limit - 1, false)
var erdos = []
var lastErdos = 0
for (p in primes) {
var iec = 10
var facti = 12
while (i < limit) {
var found = true
whileif (fact < p!c[i]) {
ifvar (Int.isPrime(pj -= fact)) {1
var foundfact = false1
var found = breaktrue
while (fact < i) {
if (!c[i - fact]) {
found = false
break
}
j = j + 1
fact = fact * j
}
if (found) {
if (i < lowerLimit) erdos.add(i)
lastErdos = i
ec = ec + 1
}
i = i + 1
fact = fact * i
}
i = (i > 2) ? i + 2 : i + 1
if (found) erdos.add(p)
}
 
var lowerLimit = 2500
Fmt.print("The $,d Erdős primes under $,d are:", erdos.count, lowerLimit)
var erdosLower = erdos.where { |e| e < lowerLimit}.toList
Fmt.tprint("$6d", erdos, 10)
Fmt.print("The $,d Erdős primes under $,d are:", erdosLower.count, lowerLimit)
Fmt.print("\nThe $,r Erdős prime is $,d.", ec, lastErdos)</syntaxhighlight>
for (chunk in Lst.chunks(erdosLower, 10)) Fmt.print("$6d", chunk)
var show = 7875
Fmt.print("\nThe $,r Erdős prime is $,d.", show, erdos[show-1])</lang>
 
{{out}}
<pre>
The 25 Erdős primes under 2,500 are:
2 101 211 367 409 419 461 557 673 709
769 937 967 1009 1201 1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
 
The 7,875th Erdős prime is 999,721.
</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 Erdos(N); \Return 'true' if N is an Erdos prime
int N, F, K;
[if not IsPrime(N) then return false;
F:= 1; K:= 1;
while F < N do
[if IsPrime(N-F) then return false;
K:= K+1; F:= F*K;
];
return true;
];
 
int Cnt, N, SN;
[Format(5, 0);
Cnt:= 0; N:= 2;
repeat if Erdos(N) then
[RlOut(0, float(N));
Cnt:= Cnt+1;
if rem(Cnt/10) = 0 then CrLf(0);
];
N:= N+1;
until N >= 2500;
CrLf(0);
Text(0, "Found "); IntOut(0, Cnt); Text(0, " Erdos primes^m^j");
Cnt:= 1; N:= 3;
repeat if Erdos(N) then [Cnt:= Cnt+1; SN:= N];
N:= N+2;
until N >= 1_000_000;
Text(0, "The "); IntOut(0, Cnt);
Text(0, "th Erdos prime is indeed "); IntOut(0, SN); CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
2 101 211 367 409 419 461 557 673 709
769 937 967 1009 1201 1259 1709 1831 1889 2141
2221 2309 2351 2411 2437
Found 25 Erdos primes
The 7875th Erdos prime is indeed 999721
</pre>
2,093

edits