Primality by Wilson's theorem: Difference between revisions
Content added Content deleted
(Added Action!) |
(Added PROMAL) |
||
Line 1,833: | Line 1,833: | ||
EOF: end wilson_100H ;</syntaxhighlight> |
EOF: end wilson_100H ;</syntaxhighlight> |
||
{{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|PROMAL}}== |
|||
<syntaxhighlight lang="promal"> |
|||
;;; Find primes using Wilson's theorem: |
|||
;;; p is prime if ( ( p - 1 )! + 1 ) mod p = 0 |
|||
;;; returns TRUE(1) if p is prime by Wilson's theorem, FALSE(0) otherwise |
|||
;;; computes the factorial mod p at each stage, so as to allow |
|||
;;; for numbers whose factorial won't fit in 16 bits |
|||
PROGRAM wilson |
|||
INCLUDE library |
|||
FUNC BYTE isWilsonPrime |
|||
ARG WORD p |
|||
WORD i |
|||
WORD fModP |
|||
BYTE result |
|||
BEGIN |
|||
fModP = 1 |
|||
IF p > 2 |
|||
FOR i = 2 TO p - 1 |
|||
fModP = ( fModP * i ) % p |
|||
IF fModP = p - 1 |
|||
result = 1 |
|||
ELSE |
|||
result = 0 |
|||
RETURN result |
|||
END |
|||
WORD i |
|||
BEGIN |
|||
FOR i = 1 TO 100 |
|||
IF isWilsonPrime( i ) |
|||
OUTPUT " #W", i |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |