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


 
: fac-mod ( n m -- r )
    >r 1 swap
    begin dup 0> while
        dup rot * r@ mod  swap 1-
    repeat drop rdrop ;
 
: ?prime ( n -- f )
    dup 1- tuck swap fac-mod = ;
 
: .primes ( n -- )
    cr 2 ?do i ?prime if i . then loop ;
 