10001th prime: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 9:
The APPROXIMATESIEVESIZEFOR operator uses this to find a rough value for size of sieve needed to contain the required number of primes.
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find the 10001st prime #
PR read "primes.incl.a68" PR
# construct a sieve of primes that should be large enough to contain 10001 primes #
Line 31:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">primes: select 2..110000 => prime?
print primes\[10000]</syntaxhighlight>
 
Line 39:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f 10001TH_PRIME.AWK
# converted from FreeBASIC
Line 79:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang=BASIC256"basic256">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 105:
 
==={{header|PureBasic}}===
<syntaxhighlight lang=PureBasic"purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 145:
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 172:
 
=={{header|C}}==
<syntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 203:
===Sieve vs Trial Division===
Comparing performance of the one-at-a-time trial division method vs the sieve of Eratosthenes method. About ten times faster for the sieve. It may appear that the sieve may be off by one, <code>pr[10000]</code> but since the array is zero based, it's the 10001st value.
<syntaxhighlight lang="csharp">using System; class Program {
 
static bool isprime(uint p ) { if ((p & 1) == 0) return p == 2;
Line 236:
 
===Alternative Trial Division Method===
<syntaxhighlight lang="csharp">using System; using System.Text; // PRIME_numb.cs russian DANILIN
namespace p10001 // 1 second 10001 104743
{ class Program // rextester.com/ZBEPGE34760
Line 260:
=={{header|C++}}==
{{libheader|Primesieve}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <locale>
 
Line 277:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
// 10001st prime. Nigel Galloway: November 22nd., 2021
printfn $"%d{Seq.item 10000 (primes32())}"
Line 286:
</pre>
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: math math.primes prettyprint ;
 
2 10,000 [ next-prime ] times .</syntaxhighlight>
Line 295:
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">
Prime(10001);
</syntaxhighlight>
Line 301:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
#include "isprime.bas"
function prime( n as uinteger ) as ulongint
Line 318:
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">nth[primes[], 10001-1]</syntaxhighlight>
{{out}}
<pre>
Line 326:
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">10 PN=1
20 P = 3
30 WHILE PN < 10001
Line 348:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 381:
 
=={{header|J}}==
<syntaxhighlight lang="j">p:10000 NB. the index starts at 0; p:0 = 2</syntaxhighlight>
{{out}}
<pre>104743</pre>
Line 387:
=={{header|Java}}==
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<syntaxhighlight lang="java">public class NthPrime {
public static void main(String[] args) {
System.out.printf("The 10,001st prime is %,d.\n", nthPrime(10001));
Line 414:
 
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
<syntaxhighlight lang="jq"># Output: a stream of the primes
def primes: 2, (range(3; infinite; 2) | select(is_prime));
 
Line 426:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">julia> using Primes
 
julia> prime(10001)
Line 432:
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Prime[10001]</syntaxhighlight>
 
{{out}}<pre>
Line 439:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">prime(10001)</syntaxhighlight>
{{out}}<pre>%1 = 104743</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 463:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">js</span> <span style="color: #0000FF;">?</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10001</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Line 473:
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">go ?=>
println(nth_prime(10001)),
 
Line 503:
=={{header|Python}}==
===Trial Division Method===
<syntaxhighlight lang="python">#!/usr/bin/python
 
def isPrime(n):
Line 528:
 
===Alternative Trial Division Method===
<syntaxhighlight lang="python">import time; max=10001; n=1; p=1; # PRIME_numb.py russian DANILIN
while n<=max: # 78081 994271 45 seconds
f=0; j=2; s = int(p**0.5) # rextester.com/AAOHQ6342
Line 548:
=={{header|QB64}}==
===Trial Division Method===
<syntaxhighlight lang="qbasic">max=10001: n=1: p=0: t=Timer ' PRIMES.bas russian DANILIN
While n <= max ' 10001 104743 0.35 seconds
f=0: j=2
Line 564:
 
===More Efficient TD Method===
<syntaxhighlight lang="qbasic">'JRace's results:
'Original version: 10001 104743 .21875
'This version: 10001 104743 .109375
Line 588:
 
=={{header|R}}==
<syntaxhighlight lang=R"r">library(primes)
nth_prime(10001)</syntaxhighlight>
{{out}}
Line 594:
 
=={{header|Racket}}==
<syntaxhighlight lang=Racket"racket">#lang racket
(require math/number-theory)
; Index starts at 0, (nth-prime 0) is 2
Line 602:
 
=={{header|Raku}}==
<syntaxhighlight lang=perl6"raku" line>say (^∞).grep( &is-prime )[10000]</syntaxhighlight>
{{out}}
<pre>104743</pre>
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/* REXX */
Parse Version v
Say v
Line 639:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 660:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require "prime"
puts Prime.lazy.drop(10_000).next</syntaxhighlight>
{{out}}
Line 666:
</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 680:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">say 10001.prime</syntaxhighlight>
{{out}}
<pre>
Line 689:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
 
Line 703:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">func IsPrime(N); \Return 'true' if odd N > 2 is prime
int N, I;
[for I:= 3 to sqrt(N) do
10,333

edits