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


import "/math" for Int
import "/fmt" for Fmt
 
var wilson = Fn.new { |p|
    if (p < 2) return false
    return (Int.factorial(p-1) + 1) % p == 0
}
 
for (p in 1..19) {
    Fmt.print("$2d -> $s", p, wilson.call(p) ? "prime" : "not prime")
}