10001th prime: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: online tag)
imported>Nmz
(Added an idiomatic awk section →‎{{header|AWK}})
Line 62: Line 62:


=={{header|AWK}}==
=={{header|AWK}}==
==== Converted from FreeBASIC ====
<syntaxhighlight lang="awk">
<syntaxhighlight lang="awk">
# syntax: GAWK -f 10001TH_PRIME.AWK
# syntax: GAWK -f 10001TH_PRIME.AWK

# converted from FreeBASIC
BEGIN {
BEGIN {
printf("%s\n",main(10001))
printf("%s\n",main(10001))
Line 95: Line 96:
}
}
</syntaxhighlight>
</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
}

# n: positive
function PrimePosPos(n, p){
n -= ( (n==1) ? p=2 : p=3 ) - 1
while (n)
if (IsOddPrime(p+=2)) n--
return p
}

BEGIN { print PrimePosPos(10001) }

</syntaxhighlight>

{{out}}
{{out}}
<pre>
<pre>