Wilson primes of order n

From Rosetta Code
Revision as of 15:54, 28 July 2021 by PureFox (talk | contribs) (Added new draft task and Wren example.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Wilson primes of order n is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

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

(n − 1)! x (p − n)! − (− 1)ⁿ.


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


Wren

Library: Wren-math
Library: Wren-big
Library: Wren-fmt

<lang ecmascript>import "/math" for Int import "/big" for BigInt import "/fmt" for Fmt

var limit = 11000 var primes = Int.primeSieve(limit) var facts = List.filled(limit, null) facts[0] = BigInt.one for (i in 1...11000) facts[i] = facts[i-1] * i var sign = 1 System.print(" n: Wilson primes") System.print("--------------------") for (n in 1..11) {

   Fmt.write("$2d:  ", n)
   sign = -sign
   for (p in primes) {
       var f = facts[n-1] * facts[p-n] - sign
       if (f.isDivisibleBy(p*p)) Fmt.write("%(p) ", p)
   }
   System.print()

}</lang>

Output:
 n:  Wilson primes
--------------------
 1:  5 13 563 
 2:  2 3 11 107 4931 
 3:  7 
 4:  10429 
 5:  5 7 47 
 6:  11 
 7:  17 
 8:  
 9:  541 
10:  11 1109 
11:  17 2713