Wilson primes of order n: Difference between revisions
Thundergnat (talk | contribs) m (→{{header|Raku}}: May as well go full Unicode) |
Thundergnat (talk | contribs) (→{{header|Raku}}: Make the performed calculation more fully resemble the given equations) |
||
Line 88: | Line 88: | ||
────────────────────'; |
────────────────────'; |
||
-> \n { printf "%3d: %s\n", n, @primes.grep( { ( |
-> \n { printf "%3d: %s\n", n, @primes.grep( ->\p { (p ≥ n) && ((n - 1)! × (p - n)! - (-1) ** n) %% p² } ).Str } for 1..11;</lang> |
||
{{out}} |
{{out}} |
||
<pre> n: Wilson primes |
<pre> n: Wilson primes |
Revision as of 17:19, 28 July 2021
- Definition
A Wilson prime of order n is a prime number p such that p² 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
Go
<lang go>package main
import (
"fmt" "math/big" "rcu"
)
func main() {
const LIMIT = 11000 primes := rcu.Primes(LIMIT) facts := make([]*big.Int, LIMIT) facts[0] = big.NewInt(1) for i := int64(1); i < LIMIT; i++ { facts[i] = new(big.Int) facts[i].Mul(facts[i-1], big.NewInt(i)) } sign := int64(1) f := new(big.Int) zero := new(big.Int) fmt.Println(" n: Wilson primes") fmt.Println("--------------------") for n := 1; n < 12; n++ { fmt.Printf("%2d: ", n) sign = -sign for _, p := range primes { if p < n { continue } f.Mul(facts[n-1], facts[p-n]) f.Sub(f, big.NewInt(sign)) p2 := int64(p * p) bp2 := big.NewInt(p2) if f.Rem(f, bp2).Cmp(zero) == 0 { fmt.Printf("%d ", p) } } fmt.Println() }
}</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
Raku
<lang perl6>sub postfix:<!> (Int $n) { (constant f = 1, |[\×] 1..*)[$n] }
my @primes = ^1.1e4 .grep: *.is-prime;
say ' n: Wilson primes ────────────────────';
-> \n { printf "%3d: %s\n", n, @primes.grep( ->\p { (p ≥ n) && ((n - 1)! × (p - n)! - (-1) ** n) %% p² } ).Str } for 1..11;</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
Wren
<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) { if (p < n) continue 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