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


on isPrime(n)
    if (n < 2) then return false
    set f to n - 1
    repeat with i from (n - 2) to 2 by -1
        set f to f * i mod n
    end repeat
 
    return ((f + 1) mod n = 0)
end isPrime
 
local output, n
set output to {}
repeat with n from 0 to 500
    if (isPrime(n)) then set end of output to n
end repeat
output