Jump to content

10001th prime: Difference between revisions

Added an idiomatic awk section →‎{{header|AWK}}
m (→‎{{header|Phix}}: online tag)
imported>Nmz
(Added an idiomatic awk section →‎{{header|AWK}})
Line 62:
 
=={{header|AWK}}==
#==== convertedConverted 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
}
 
# 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}}
<pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.