Wilson primes of order n: Difference between revisions
(→{{header|Wren}}: Bug fix, results unaffected.) |
(Added Go) |
||
Line 17: | Line 17: | ||
:* [[Primality by Wilson's theorem]] |
:* [[Primality by Wilson's theorem]] |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|Go-rcu}} |
|||
<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> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
Revision as of 16:39, 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
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