Primality by Wilson's theorem: Difference between revisions

Added Algol W
m (added whitespace before the TOC.)
(Added Algol W)
Line 117:
241
251
</pre>
 
=={{header|ALGOL W}}==
As with the APL, Tiny BASIC (and possibly others) samples, this computes the factorials mod p at each multiplication to avoid needing numbers larger than the 32 bit limit.
<lang algolw>begin
% find primes using Wilson's theorem: %
% p is prime if ( ( p - 1 )! + 1 ) mod p = 0 %
 
% returns true if n is a prime by Wilson's theorem, false otherwise %
% computes the factorial mod p at each stage, so as to %
% allow numbers whose factorial won't fit in 32 bits %
logical procedure isWilsonPrime ( integer value n ) ;
if n < 2 then false
else begin
integer factorialModN;
factorialModN := 1;
for i := 2 until n - 1 do factorialModN := ( factorialModN * i ) rem n;
factorialModN = n - 1
end isWilsonPrime ;
 
for i := 1 until 100 do if isWilsonPrime( i ) then writeon( i_w := 1, s_w := 0, " ", i );
end.</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>
 
Line 141 ⟶ 166:
 
<pre> ⎕CT←0 ⋄ naiveWilson {(⍺⍺¨⍵)/⍵} ⍳20
2 3 5 7 11 13 17 19 20</pre>
 
=={{header|C}}==
3,048

edits