10001th prime: Difference between revisions

m
typo
m (typo)
 
(28 intermediate revisions by 15 users not shown)
Line 62:
 
=={{header|AWK}}==
==== Converted from FreeBASIC ====
<syntaxhighlight lang="awk">
# syntax: GAWK -f 10001TH_PRIME.AWK
 
# converted from FreeBASIC
BEGIN {
printf("%s\n",main(10001))
Line 95 ⟶ 96:
}
</syntaxhighlight>
 
==== Idiomatic ====
<syntaxhighlight lang="awk">
# awk -f 10001prime.awk
 
# n: odd number and n>8
function IsOddPrime(n, i,limit) {
limit = int(sqrt(n))
for (i=3;i <= limit;i+=2)
if (n%i==0) return 0
return 1
}
 
# pos: positive
function PrimePosition(pos, prime){
pos -= ( (pos==1) ? prime=2 : prime=3 ) - 1
while (pos)
if (IsOddPrime(prime+=2)) pos--
return prime
}
 
BEGIN { print PrimePosition(10001) }
 
</syntaxhighlight>
 
{{out}}
<pre>
Line 126 ⟶ 152:
print prime(10001)
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|FreeBASIC}}===
Line 143 ⟶ 173:
</syntaxhighlight>
{{out}}<pre>104743</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">'Use "isprime.bas"
 
Public Sub Main()
 
Print prime(10001)
End
 
Function prime(n As Integer) As Long
 
If n = 1 Then Return 2
Dim p As Integer = 3, pn As Integer = 1
While pn < n
If isPrime(p) Then pn += 1
p += 2
Wend
Return p - 2
End Function </syntaxhighlight>
{{out}}
<pre>104743</pre>
 
==={{header|GW-BASIC}}===
Line 247 ⟶ 300:
{{out}}
<pre>10001 104743 0.11 seconds</pre>
 
==={{header|True BASIC}}===
====Trial Division Method====
{{trans|QB64}}
<syntaxhighlight lang="qbasic">LET max = 10001
LET n = 1
LET p = 0
DO WHILE n <= max
LET f = 0
LET j = 2
DO WHILE f < 1
IF j >= p^0.5 THEN LET f = 2
IF REMAINDER(p, j) = 0 THEN LET f = 1
LET j = j+1
LOOP
IF f <> 1 THEN LET n = n+1
LET p = p+1
LOOP
PRINT p-1
END</syntaxhighlight>
{{out}}
<pre>104743</pre>
 
==={{header|Yabasic}}===
Line 392 ⟶ 467:
The 10,001st prime is 104,743.
</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">import 'dart:math';
 
bool isPrime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) return false;
}
return true;
}
 
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;
}
 
void main() {
print(prime(10001));
}</syntaxhighlight>
{{out}}
<pre>104743</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|none}}
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: integer): boolean;
{Fast, optimised prime test}
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 Find10001thPrime: integer;
{Return the 10,001th prime number}
var Count: integer;
begin
Count:=1;
Result:= 3;
while true do
begin
if IsPrime(Result) then Count:= Count+1;
if Count=10001 then exit;
Inc(Result,2);
end;
end;
</syntaxhighlight>
 
{{out}}
<pre>
10,001th Prime= 104,743
</pre>
 
=={{header|D}}==
 
{{trans|C}}
 
<syntaxhighlight lang="d">
import std.stdio;
 
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 ) {
if(n==1) return 2;
int p, pn=1;
for(p=3;pn<n;p+=2) {
if(isprime(p)) pn++;
}
return p-2;
}
 
void main()
{
writeln(prime(10_001));
}
</syntaxhighlight>
{{out}}<pre>104743</pre>
 
=={{header|Erlang}}==
Line 423 ⟶ 612:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
funcfastfunc isPrimeisprim num . result$ .
if num < 2
result$ = "false"
break 1
.
if num mod 2 = 0 and num > 2
result$return = "false"0
break 1
.
for i = 3 to sqrt num
while i <= sqrt num
if num mod i = 0
result$return = "false"0
break 2
.
i += 2
.
result$return = "true"1
.
currentPrimei = 2
repeat
# Because 2 is the 1st prime number,
if isprim i = 1
# We will start counting primes from the 2nd
for i = 2 to 10001 nprim += 1
repeat
currentPrime += 1
call isPrime currentPrime result$
until result$ = "true"
.
until nprim = 10001
i += 1
.
print currentPrimei
</syntaxhighlight>
{{out}}
Line 475 ⟶ 658:
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermatpascal">
Prime(10001);
</syntaxhighlight>
Line 483 ⟶ 666:
<syntaxhighlight lang="frink">nth[primes[], 10001-1]</syntaxhighlight>
{{out}}
<pre>
104743
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
 
local fn Prime( n as NSUInteger ) as long
long p = 3, pn = 1
if n = 1 then exit fn = 2
while ( pn < n )
if fn IsPrime( p ) then pn++
p += 2
wend
p -= 2
end fn = p
 
print fn Prime(10001)
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
104743
Line 600 ⟶ 819:
{{out}}<pre>
104743
</pre>
 
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
primes_first_n(n) := (
if n<=1 then
[]
else (
L : [2],
for i:2 thru n do (
L : append(L, [next_prime(last(L))])
),
L
)
)$
last(primes_first_n(10001));
</syntaxhighlight>
{{out}}<pre>
104743
</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
isPrime = function(n)
s = floor(n ^ 0.5)
for i in range(2, s)
if n % i == 0 then return false
end for
return true
end function
 
tt = time
c = 2
p = 2
lastPrime = 2
while c < 10001
p += 1
if isPrime(p) then
c += 1
end if
end while
print p
</syntaxhighlight>
{{out}}
<pre>
104743</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">func isPrime(n: Positive): bool =
## Return true if "n" is prime.
## Assume n >= 5 and n not multiple of 2 and 3.
var d = 5
var step = 2
while d * d <= n:
if n mod d == 0:
return false
inc d, step
step = 6 - step
result = true
 
iterator primes(): tuple[rank, value: int] =
## Yield the primes preceded by their rank in the sequence.
yield (1, 2)
yield (2, 3)
var idx = 2
var n = 5
var step = 2
while true:
if n.isPrime:
inc idx
yield (idx, n)
inc n, step
step = 6 - step
 
for (i, n) in primes():
if i == 10001:
echo "10001st prime: ", n
break
</syntaxhighlight>
 
{{out}}
<pre>10001st prime: 104743
</pre>
 
Line 612 ⟶ 914:
<syntaxhighlight lang="parigp">prime(10001)</syntaxhighlight>
{{out}}<pre>%1 = 104743</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program nth_prime;
 
Function nthprime(x : uint32): uint32;
Var primes : array Of uint32;
i,pr,count : uint32;
found : boolean;
Begin
setlength(primes, x);
primes[1] := 2;
count := 1;
i := 3;
Repeat
found := FALSE;
pr := 0;
Repeat
inc(pr);
found := i Mod primes[pr] = 0;
Until (primes[pr]*primes[pr] > i) Or found;
If Not found Then
Begin
inc(count);
primes[count] := i;
End;
inc(i,2);
Until count = x;
nthprime := primes[x];
End;
 
Begin
writeln(nthprime(10001));
End.
</syntaxhighlight>
{{out}}
<pre>
104743
</pre>
 
=={{header|Perl}}==
Line 634 ⟶ 976:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<syntaxhighlight lang="phix">
<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>
with js ?get_prime(10001)
<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
104743
</pre>
 
 
=={{header|Picat}}==
Line 738 ⟶ 1,080:
The 10001st prime number is the 10000th odd prime number.
 
<code>isprimeprime</code> is defined at [[PrimalityMiller–Rabin byprimality trial divisiontest#Quackery]].
 
<syntaxhighlight lang="Quackery"> 1 10000 times [ 2 + dup isprimeprime until ] echo</syntaxhighlight>
 
{{out}}
Line 816 ⟶ 1,158:
The 10001th prime is: 104743
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« 2
1 10000 '''START''' NEXTPRIME '''NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>1: 104743
</pre>
 
Line 824 ⟶ 1,175:
<pre>104743
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 862 ⟶ 1,214:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 901 ⟶ 1,253:
104743
</pre>
=={{header|D}}==
<syntaxhighlight lang="d">
import std.stdio;
 
 
int isprime( int p ) {
=={{header|Zig}}==
int i;
<syntaxhighlight lang="zig">
const std = @import("std");
if(p==2) return 1;
const stdout = @import("std").io.getStdOut().writer();
if(!(p%2)) return 0;
 
const limit = 10001;
for(i=3; i*i<=p; i+=2) {
 
if(!(p%i)) return 0;
fn isPrime(x: usize) bool {
if (x % 2 == 0) return false;
 
var i: usize = 3;
while (i * i <= x) : (i += 2) {
if (x % i == 0) return false;
}
return true;
return 1;
}
 
intpub primefn main() int n )!void {
var cnt: usize = 0;
if(n==1)var returnlast: usize = 20;
var n: usize = 1;
 
int p, pn=1;
while (cnt < limit) : (n += 1) {
for if (isPrime(p=3;pn<n;p+=2)) {
if(isprime(p)) pn+ cnt += 1;
last = n;
}
}
try stdout.print("{d}st prime number is: {d}\n", .{ limit, last });
return p-2;
}
</syntaxhighlight>
 
{{out}}
void main()
<pre>
{
10001st prime number is: 104743
writeln(prime(10_001));
</pre>
}
</syntaxhighlight>
{{out}}<pre>104743</pre>
20

edits