10001th prime: Difference between revisions

m
typo
m (typo)
 
(14 intermediate revisions by 8 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 632 ⟶ 658:
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermatpascal">
Prime(10001);
</syntaxhighlight>
Line 794 ⟶ 820:
104743
</pre>
 
 
=={{header|Maxima}}==
Line 813 ⟶ 840:
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}}==
Line 861 ⟶ 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 883 ⟶ 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 987 ⟶ 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 1,065 ⟶ 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 1,073 ⟶ 1,175:
<pre>104743
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 1,111 ⟶ 1,214:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 1,149 ⟶ 1,252:
<pre>
104743
</pre>
 
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = @import("std").io.getStdOut().writer();
 
const limit = 10001;
 
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;
}
 
pub fn main() !void {
var cnt: usize = 0;
var last: usize = 0;
var n: usize = 1;
 
while (cnt < limit) : (n += 1) {
if (isPrime(n)) {
cnt += 1;
last = n;
}
}
try stdout.print("{d}st prime number is: {d}\n", .{ limit, last });
}
</syntaxhighlight>
 
{{out}}
<pre>
10001st prime number is: 104743
</pre>
19

edits