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


factorial: function [x]-> product 1..x
 
wprime?: function [n][
    if n < 2 -> return false
    zero? mod add factorial sub n 1 1 n
]
 
print "Primes below 20 via Wilson's theorem:"
print select 1..20 => wprime?