Definition
A Wilson prime of order n is a prime number   p   such that   p2   exactly divides:

     (n − 1)! × (p − n)! − (− 1)n 



If   n   is   1,   the latter formula reduces to the more familiar:   (p - n)! + 1   where the only known examples for   p   are   5,   13,   and   563.



Task
Calculate and show on this page the Wilson primes, if any, for orders n = 1 to 11 inclusive and for primes p < 18   or,

if your language supports big integers, for p < 11,000.



Related task
 Primality by Wilson's theorem


#include "isprime.bas"
 
function is_wilson( n as uinteger, p as uinteger ) as boolean
    'tests if p^2 divides (n-1)!(p-n)! - (-1)^n
    'does NOT test the primality of p; do that first before you call this!
    'using mods no big nums are required
    if p<n then return false
    dim as uinteger prod = 1, i, p2 = p^2
    for i = 1 to n-1
        prod = (prod*i) mod p2
    next i
    for i = 1 to p-n
        prod = (prod*i) mod p2
    next i
    prod = (p2 + prod - (-1)^n) mod p2
    if prod = 0 then return true else return false
end function
 
print "n:      Wilson primes"
print "--------------------"
for n as uinteger = 1 to 11
    print using "##      ";n;
        for p as uinteger = 3 to 10099 step 2
        if isprime(p) andalso is_wilson(n, p) then print p;" ";
    next p
    print
next n
 