Task

Write a boolean function that tells whether a given integer is prime using Wilson's theorem.

By Wilson's theorem, a number p is prime if and only if p divides (p - 1)! + 1.

Remember that 1 and all non-positive integers are not prime.



See also

 Cut-the-knot: Wilson's theorem.
 Wikipedia: Wilson's theorem


 
load "stdlib.ring"
 
decimals(0)
limit = 19
 
for n = 2 to limit
    fact = factorial(n-1) + 1
    see "Is " + n + " prime: "
    if fact % n = 0
       see "1" + nl
    else
       see "0" + nl
    ok
next
 