10001th prime: Difference between revisions

Content added Content deleted
m (→‎{{header|QB64}}: enabled syntax hi-lighting by changing lang tag from "QB64" to "qbasic")
m (syntax highlighting fixup automation)
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}}
<langsyntaxhighlight 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 23:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 31:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>primes: select 2..110000 => prime?
print primes\[10000]</langsyntaxhighlight>
 
{{out}}
Line 39:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f 10001TH_PRIME.AWK
# converted from FreeBASIC
Line 71:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 79:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight lang=BASIC256>function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 102:
 
print prime(10001)
end</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight lang=PureBasic>Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 142:
n = prime(10001)
PrintN(Str(n))
CloseConsole()</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang=yabasic>sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 168:
 
print prime(10001)
end</langsyntaxhighlight>
 
 
=={{header|C}}==
<langsyntaxhighlight lang=c>#include<stdio.h>
#include<stdlib.h>
 
Line 197:
printf( "%d\n", prime(10001) );
return 0;
}</langsyntaxhighlight>
{{out}}<pre>104743</pre>
 
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.
<langsyntaxhighlight lang=csharp>using System; class Program {
 
static bool isprime(uint p ) { if ((p & 1) == 0) return p == 2;
Line 230:
t = pr[10000]; sw.Stop();
Console.Write(" {0:n0} {1} μs {2:0.000} times faster", t,
(e2 = sw.Elapsed.TotalMilliseconds) * 1000.0, e1 / e2); } }</langsyntaxhighlight>
{{out|Output @ Tio.run}}
<pre>One-at-a-time trial division vs sieve of Eratosthenes
Line 236:
 
===Alternative Trial Division Method===
<langsyntaxhighlight lang=csharp>using System; using System.Text; // PRIME_numb.cs russian DANILIN
namespace p10001 // 1 second 10001 104743
{ class Program // rextester.com/ZBEPGE34760
Line 254:
Console.Write("{0} {1}", n-1, p-1);
Console.ReadKey();
}}}</langsyntaxhighlight>
{{out}}
<pre>104743</pre>
Line 260:
=={{header|C++}}==
{{libheader|Primesieve}}
<langsyntaxhighlight lang=cpp>#include <iostream>
#include <locale>
 
Line 268:
std::cout.imbue(std::locale(""));
std::cout << "The 10,001st prime is " << primesieve::nth_prime(10001) << ".\n";
}</langsyntaxhighlight>
 
{{out}}
Line 277:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang=fsharp>
// 10001st prime. Nigel Galloway: November 22nd., 2021
printfn $"%d{Seq.item 10000 (primes32())}"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 286:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: math math.primes prettyprint ;
 
2 10,000 [ next-prime ] times .</langsyntaxhighlight>
{{out}}
<pre>
Line 295:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang=fermat>
Prime(10001);
</syntaxhighlight>
</lang>
{{out}}<pre>104743</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>
#include "isprime.bas"
function prime( n as uinteger ) as ulongint
Line 314:
 
print prime(10001)
</syntaxhighlight>
</lang>
{{out}}<pre>104743</pre>
 
=={{header|Frink}}==
<langsyntaxhighlight lang=frink>nth[primes[], 10001-1]</langsyntaxhighlight>
{{out}}
<pre>
Line 326:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang=gwbasic>10 PN=1
20 P = 3
30 WHILE PN < 10001
Line 344:
170 IF I*I<=P THEN GOTO 150
180 Q = 1
190 RETURN</langsyntaxhighlight>
{{out}}<pre>104743</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 377:
fmt.Println(final)
}
</syntaxhighlight>
</lang>
{{out}}<pre>104743</pre>
 
=={{header|J}}==
<langsyntaxhighlight lang=j>p:10000 NB. the index starts at 0; p:0 = 2</langsyntaxhighlight>
{{out}}
<pre>104743</pre>
Line 387:
=={{header|Java}}==
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<langsyntaxhighlight lang=java>public class NthPrime {
public static void main(String[] args) {
System.out.printf("The 10,001st prime is %,d.\n", nthPrime(10001));
Line 400:
return prime;
}
}</langsyntaxhighlight>
 
{{out}}
Line 414:
 
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
<langsyntaxhighlight lang=jq># Output: a stream of the primes
def primes: 2, (range(3; infinite; 2) | select(is_prime));
 
# The task
# jq uses an index-origin of 1 and so:
nth(10000; primes)</langsyntaxhighlight>
{{out}}
<pre>
Line 426:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>julia> using Primes
 
julia> prime(10001)
104743
</syntaxhighlight>
</lang>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang =Mathematica>Prime[10001]</langsyntaxhighlight>
 
{{out}}<pre>
Line 439:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang =parigp>prime(10001)</langsyntaxhighlight>
{{out}}<pre>%1 = 104743</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang=perl>use strict;
use warnings;
use feature 'say';
Line 457:
# or if for some reason you want the answer quickly
use ntheory 'nth_prime';
say nth_prime(10_001);</langsyntaxhighlight>
{{out}}
<pre>104743
Line 463:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 473:
 
=={{header|Picat}}==
<langsyntaxhighlight lang=Picat>go ?=>
println(nth_prime(10001)),
 
Line 495:
prime(Num).
next_prime2(Num, P) :-
next_prime2(Num+1,P).</langsyntaxhighlight>
 
{{out}}
Line 503:
=={{header|Python}}==
===Trial Division Method===
<langsyntaxhighlight lang=python>#!/usr/bin/python
 
def isPrime(n):
Line 523:
 
if __name__ == '__main__':
print(prime(10001))</langsyntaxhighlight>
{{out}}
<pre>104743</pre>
 
===Alternative Trial Division Method===
<langsyntaxhighlight 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 542:
p+=1
print(n-1,p-1)
print(time.perf_counter())</langsyntaxhighlight>
{{out}}
<pre>10001 104743 7 seconds</pre>
Line 548:
=={{header|QB64}}==
===Trial Division Method===
<langsyntaxhighlight 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 559:
p=p+1
Wend
Print n-1, p-1, Timer-t</langsyntaxhighlight>
{{out}}
<pre>10001 104743 0.35 seconds</pre>
 
===More Efficient TD Method===
<langsyntaxhighlight lang=qbasic>'JRace's results:
'Original version: 10001 104743 .21875
'This version: 10001 104743 .109375
Line 583:
p=p+1
Wend
Print n-1, p-1, Timer - t</langsyntaxhighlight>
{{out}}
<pre>10001 104743 0.11 seconds</pre>
 
=={{header|R}}==
<langsyntaxhighlight lang=R>library(primes)
nth_prime(10001)</langsyntaxhighlight>
{{out}}
<pre>104743</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang=Racket>#lang racket
(require math/number-theory)
; Index starts at 0, (nth-prime 0) is 2
(nth-prime 10000)</langsyntaxhighlight>
{{out}}
<pre>104743</pre>
 
=={{header|Raku}}==
<langsyntaxhighlight lang=perl6>say (^∞).grep( &is-prime )[10000]</langsyntaxhighlight>
{{out}}
<pre>104743</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang=rexx>/* REXX */
Parse Version v
Say v
Line 628:
End
End
Say z n time('E')</langsyntaxhighlight>
{{out}}
<pre>H:\>rexx prime10001
Line 639:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
load "stdlib.ring"
see "working..." + nl
Line 651:
 
see "" + num + nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 660:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=ruby>require "prime"
puts Prime.lazy.drop(10_000).next</langsyntaxhighlight>
{{out}}
<pre>104743
</pre>
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>// [dependencies]
// primal = "0.3"
 
Line 672:
let p = primal::StreamingSieve::nth_prime(10001);
println!("The 10001st prime is {}.", p);
}</langsyntaxhighlight>
 
{{out}}
Line 680:
 
=={{header|Sidef}}==
<syntaxhighlight lang =ruby>say 10001.prime</langsyntaxhighlight>
{{out}}
<pre>
Line 689:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "./math" for Int
import "./fmt" for Fmt
 
Line 695:
var limit = (n.log * n * 1.2).floor // should be enough
var primes = Int.primeSieve(limit)
Fmt.print("The $,r prime is $,d.", n, primes[n-1])</langsyntaxhighlight>
 
{{out}}
Line 703:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>func IsPrime(N); \Return 'true' if odd N > 2 is prime
int N, I;
[for I:= 3 to sqrt(N) do
Line 722:
];
IntOut(0, N);
]</langsyntaxhighlight>
 
{{out}}