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


-- primality by Wilson's theorem
 
function isWilsonPrime( n )
    local fmodp = 1
    for i = 2, n - 1 do
        fmodp = fmodp * i
        fmodp = fmodp % n
    end
    return fmodp == n - 1
end
 
for n = -1, 100 do
    if isWilsonPrime( n ) then
       io.write( " " .. n )
    end
end