Primality by Wilson's theorem: Difference between revisions

Content added Content deleted
(Added GAP)
(added AWK)
Line 319: Line 319:
<pre>Primes below 20 via Wilson's theorem:
<pre>Primes below 20 via Wilson's theorem:
2 3 5 7 11 13 17 19</pre>
2 3 5 7 11 13 17 19</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f PRIMALITY_BY_WILSONS_THEOREM.AWK
# converted from FreeBASIC
BEGIN {
start = 2
stop = 200
for (i=start; i<=stop; i++) {
if (is_wilson_prime(i)) {
printf("%5d%1s",i,++count%10?"":"\n")
}
}
printf("\nWilson primality test range %d-%d: %d\n",start,stop,count)
exit(0)
}
function is_wilson_prime(n, fct,i) {
fct = 1
for (i=2; i<=n-1; i++) {
# because (a mod n)*b = (ab mod n)
# it is not necessary to calculate the entire factorial
fct = (fct * i) % n
}
return(fct == n-1)
}
</lang>
{{out}}
<pre>
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199
Wilson primality test range 2-200: 46
</pre>


=={{header|C}}==
=={{header|C}}==