I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

# 10001th prime

10001th prime is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

## ALGOL 68

Chebyshev showed that the limit of pi( n ) / ( n / ln(n) ) as n approaches infinity is 1 ( pi( n ) is the number of primes up to n ).
The APPROXIMATESIEVESIZEFOR operator uses this to find a rough value for size of sieve needed to contain the required number of primes.

`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 #    INT required prime = 10 001;    []BOOL prime = PRIMESIEVE APPROXIMATESIEVESIZEFOR required prime;    INT p count := 1;    FOR n FROM 3 BY 2 WHILE p count < required prime DO        IF prime[ n ] THEN            p count +:= 1;            IF p count = required prime THEN                print( ( "Prime ", whole( required prime, 0 ), " is ", whole( n, 0 ) ) )            FI        FI    ODEND`
Output:
```Prime 10001 is 104743
```

## C

`#include<stdio.h>#include<stdlib.h> int isprime( int p ) {    int i;    if(p==2) return 1;    if(!(p%2)) return 0;    for(i=3; i*i<=p; i+=2) {       if(!(p%i)) return 0;    }    return 1;} int prime( int n ) {    int p, pn=1;    if(n==1) return 2;    for(p=3;pn<n;p+=2) {        if(isprime(p)) pn++;    }    return p-2;} int main(void) {    printf( "%d\n", prime(10001) );    return 0;}`
Output:
`104743`

## C#

Comparing performance of the one-at-a-time 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, `pr` but since the array is zero based, it's the 10001st value.

`using System; class Program {   static bool isprime(uint p ) { if ((p & 1) == 0) return p == 2;    if ((p % 3) == 0) return p == 3;    for (uint i = 5, d = 4; i * i <= p; i += (d = 6 - d))      if (p % i == 0) return false; return true; }   static uint prime(uint n) { uint p, d, pn;    for (p = 5, d = 4, pn = 2; pn < n; p += (d = 6 - d))      if (isprime(p)) pn++; return p - d; }   static void Main(string[] args) {    Console.WriteLine("One-at-a-time vs sieve of Eratosthenes");    var sw = System.Diagnostics.Stopwatch.StartNew();    var t = prime(10001); sw.Stop(); double e1, e2;    Console.Write("{0:n0} {1} ms", prime(10001),      e1 = sw.Elapsed.TotalMilliseconds);    sw.Restart(); uint n = 105000, i, j; var pr = new uint;    pr = 2; pr = 3; uint pc = 2, r, d, ii;    var pl = new bool[n + 1]; r = (uint)Math.Sqrt(n);    for (i = 9; i < n; i += 6) pl[i] = true;    for (i = 5, d = 4; i <= r; i += (d = 6 - d)) if (!pl[i]) {      pr[pc++] = i; for (j = i * i, ii = i << 1; j <= n; j += ii)        pl[j] = true; }    for ( ;i <= n; i += (d = 6 - d)) if (!pl[i]) pr[pc++] = i;    t = pr; sw.Stop();    Console.Write("  {0:n0} {1} μs  {2:0.000} times faster", t,      (e2 = sw.Elapsed.TotalMilliseconds) * 1000.0, e1 / e2); } }`
Output @ Tio.run:
```One-at-a-time vs sieve of Eratosthenes
104,743 3.8943 ms  104,743 357.9 μs  10.881 times faster```

## C++

Library: Primesieve
`#include <iostream>#include <locale> #include <primesieve.hpp> int main() {    std::cout.imbue(std::locale(""));    std::cout << "The 10,001st prime is " << primesieve::nth_prime(10001) << ".\n";}`
Output:
```The 10,001st prime is 104,743.
```

## F#

This task uses Extensible Prime Generator (F#)

` // 10001st prime. Nigel Galloway: November 22nd., 2021printfn \$"%d{Seq.item 10000 (primes32())}" `
Output:
```104743
```

## Factor

`USING: math math.primes prettyprint ; 2 10,000 [ next-prime ] times .`
Output:
```104743
```

## Fermat

` Prime(10001); `
Output:
`104743`

## FreeBASIC

` #include "isprime.bas"function prime( n as uinteger ) as ulongint    if n=1 then return 2    dim as integer p=3, pn=1    while pn<n        if isprime(p) then pn + = 1        p += 2    wend    return p-2end function print prime(10001) `
Output:
`104743`

## GW-BASIC

`10 PN=120 P = 330 WHILE PN < 1000140 GOSUB 10050 IF Q = 1 THEN PN = PN + 160 P = P + 270 WEND80 PRINT P-290 END100 REM tests if a number is prime110 Q=0120 IF P = 2 THEN Q =  1: RETURN130 IF P=3 THEN Q=1:RETURN140 I=1150 I=I+2160 IF INT(P/I)*I = P THEN RETURN170 IF I*I<=P THEN GOTO 150180 Q = 1190 RETURN`
Output:
`104743`

## J

`p:10000      NB. the index starts at 0; p:0 = 2`
Output:
`104743`

## Java

Uses the PrimeGenerator class from Extensible prime generator#Java.

`public class NthPrime {    public static void main(String[] args) {        System.out.printf("The 10,001st prime is %,d.\n", nthPrime(10001));    }     private static int nthPrime(int n) {        assert n > 0;        PrimeGenerator primeGen = new PrimeGenerator(10000, 100000);        int prime = primeGen.nextPrime();        while (--n > 0)            prime = primeGen.nextPrime();        return prime;    }}`
Output:
```The 10,001st prime is 104,743.
```

## Julia

`julia> using Primes julia> prime(10001)104743 `

## PARI/GP

`prime(10001)`
Output:
`%1 = 104743`

## Perl

Library: ntheory
`use strict;use warnings;use feature 'say'; # the lengthy wait increases the delight in finally seeing the answermy(\$n,\$c) = (1,0);while () {    \$c++ if (1 x ++\$n) !~ /^(11+)\1+\$/;    \$c == 10_001 and say \$n and last;} # or if for some reason you want the answer quicklyuse ntheory 'nth_prime';say nth_prime(10_001);`
Output:
```104743
104743```

## Phix

```with js ?get_prime(10001)
```
Output:
```104743
```

## Raku

`say (^∞).grep( &is-prime )`
Output:
`104743`

## Ring

` load "stdlib.ring"see "working..." + nlnum = 0 pr = 0 limit = 10001 while true      num++      if isprime(num) pr++ ok      if pr = limit exit okend see "" + num + nl + "done..." + nl `
Output:
```working...
The 10001th prime is: 104743
done...
```

## Rust

`// [dependencies]// primal = "0.3" fn main() {    let p = primal::StreamingSieve::nth_prime(10001);    println!("The 10001st prime is {}.", p);}`
Output:
```The 10001st prime is 104743.
```

## Wren

Library: Wren-math
Library: Wren-fmt
`import "./math" for Intimport "./fmt" for Fmt var n = 10001var limit = (n.log * n * 1.2).floor  // should be enoughvar primes = Int.primeSieve(limit)Fmt.print("The \$,r prime is \$,d.", n, primes[n-1])`
Output:
```The 10,001st prime is 104,743.
```