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


function wilson_prime(n)
    fct = 1
    for i = 2 to n-1
        fct = (fct * i) mod n
    next i
    if fct = n-1 then return True else return False
end function
 
print "Primes below 100" & Chr(10)
for i = 2 to 100
    if wilson_prime(i) then print i; "   ";
next i
end