Primality by Wilson's theorem: Difference between revisions

Content added Content deleted
(→‎{{header|ALGOL W}}: indentation)
(Add Cowgol)
Line 314:
 
The Wilson's theorem method is better suited for computing single primes, as the SoE method causes one to compute all the primes up to the desired item. In this C# implementation, a running factorial is maintained to help the Wilson's theorem method be a little more efficient. The stand-alone results show that when having to compute a BigInteger factorial for every primality test, the performance drops off considerably more.
 
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
 
# Wilson primality test
sub wilson(n: uint32): (out: uint8) is
out := 0;
if n >= 2 then
var facmod: uint32 := 1;
var ct := n - 1;
while ct > 0 loop
facmod := (facmod * ct) % n;
ct := ct - 1;
end loop;
if facmod + 1 == n then
out := 1;
end if;
end if;
end sub;
 
# Print primes up to 100 according to Wilson
var i: uint32 := 1;
while i < 100 loop
if wilson(i) == 1 then
print_i32(i);
print_char(' ');
end if;
i := i + 1;
end loop;
print_nl();</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</pre>
 
=={{header|Erlang}}==