Primality by Wilson's theorem: Difference between revisions

Content deleted Content added
Add swift
Added Algol 58
Line 187:
367 373 379 383 389 397 401 409 419 421 431 433
439 443 449 457 461 463 467 479 487 491 499
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
As with many samples on this page, applies the modulo operation at each step in calculating the factorial, to avoid needing large integeres.
<lang algol68>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 #
PROC is wilson prime = ( INT p )BOOL:
IF p < 2 THEN FALSE
ELSE
INT factorial mod p := 1;
FOR i FROM 2 TO p - 1 DO factorial mod p *:= i MODAB p OD;
factorial mod p = p - 1
FI # is wilson prime # ;
FOR i TO 100 DO IF is wilson prime( i ) THEN print( ( " ", whole( i, 0 ) ) ) FI OD
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>