Erdős-primes: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 19:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 62:
I count == 7875
print("\nThe 7875th Erdos prime is "p‘.’)
L.break</langsyntaxhighlight>
 
{{out}}
Line 76:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE Func IsErdosPrime(INT x BYTE ARRAY primes)
Line 111:
OD
PrintF("%E%EThere are %I Erdos primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Erd%C5%91s-primes.png Screenshot from Atari 8-bit computer]
Line 121:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find Erdős primes - primes p such that 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:
Line 156:
OD;
print( ( newline, "Found ", whole( e count, 0 ), " Erdos primes" ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 165:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">factorials: map 1..20 => [product 1..&]
erdos?: function [x][
if not? prime? x -> return false
Line 177:
 
loop split.every:10 select 2..2500 => erdos? 'a ->
print map a => [pad to :string & 5]</langsyntaxhighlight>
 
{{out}}
Line 186:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ERDOS-PRIMES.AWK
# converted from FreeBASIC
Line 224:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 233:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System; using static System.Console;
class Program {
const int lmt = (int)1e6, first = 2500; static int[] f = new int[10];
Line 255:
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 268:
=={{header|C++}}==
{{libheader|Primesieve}}
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iomanip>
#include <iostream>
Line 317:
std::wcout << L"\n\nThe " << max_count << L"th Erd\x151s prime is " << p << L".\n";
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 331:
=={{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 337:
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 346:
=={{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 363:
 
7874 erdős lnth commas
"The 7,875th Erdős prime is %s.\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 374:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 425:
 
2500 print_erdos_primes
bye</langsyntaxhighlight>
 
{{out}}
Line 438:
=={{header|FreeBASIC}}==
I won't bother reproducing a primality-testing function; use the one from [[Primality_by_trial_division#FreeBASIC]].
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function is_erdos_prime( p as uinteger ) as boolean
Line 457:
if i<2500 or c=7875 then print c, i
end if
wend</langsyntaxhighlight>
{{out}}<pre>1 2
2 101
Line 489:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 579:
show := 7875
fmt.Printf("\n\nThe %s Erdős prime is %s.\n", commatize(show), commatize(erdos[show-1]))
}</langsyntaxhighlight>
 
{{out}}
Line 594:
Implementation:
 
<langsyntaxhighlight Jlang="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
Line 602:
 
NB. erdos primes less than j
erdosprs=: {{ (#~ j&>);erdospr&.>i.>.!inv j=. y }}</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="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</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class ErdosPrimes {
Line 665:
return sieve;
}
}</langsyntaxhighlight>
 
{{out}}
Line 683:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
 
def is_prime:
Line 700:
| . * . > $n
end;
</syntaxhighlight>
</lang>
'''Erdős-primes'''
<langsyntaxhighlight lang="jq">
def is_Erdos:
. as $p
Line 717:
# 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 756:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Formatting
 
function isErdős(p::Integer)
Line 773:
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]
Line 780:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ErdosPrimeQ]
ErdosPrimeQ[p_Integer] := Module[{k},
If[PrimeQ[p],
Line 796:
Length[sel]
sel = Select[Range[999999], ErdosPrimeQ];
{Length[sel], Last[sel]}</langsyntaxhighlight>
{{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}
Line 803:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, sets, strutils, sugar
 
const N = 1_000_000
Line 854:
erdös7875 = p
break
echo "\nThe 7875th Erdös prime is $#.".format(erdös7875)</langsyntaxhighlight>
 
{{out}}
Line 866:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 886:
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 894:
 
=={{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 914:
<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 925:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my @factorial = 1, |[\*] 1..*;
Line 932:
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 940:
 
=={{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 990:
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 1,011:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,052:
println!("\nThe 7875th Erd\u{151}s prime is {}.", p);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,065:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_erdos_prime(p) {
 
return true if p==2
Line 1,081:
 
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>
Line 1,091:
Can't manage the stretch goal because integers are limited to signed 16 bit.
 
<langsyntaxhighlight lang="tinybasic"> LET P = 1
10 IF P > 2 THEN LET P = P + 2
IF P < 3 THEN LET P = P + 1
Line 1,128:
IF A=K THEN RETURN
LET A = A + 1
GOTO 2010</langsyntaxhighlight>
{{out}}<pre>1 2
2 101
Line 1,157:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/seq" for Lst
import "/fmt" for Fmt
Line 1,183:
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])</langsyntaxhighlight>
 
{{out}}
Line 1,196:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 1,236:
Text(0, "The "); IntOut(0, Cnt);
Text(0, "th Erdos prime is indeed "); IntOut(0, SN); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
10,327

edits