How to resolve the algorithm Wilson primes of order n step by step in the Julia programming language
How to resolve the algorithm Wilson primes of order n step by step in the Julia programming language
Table of Contents
Problem Statement
A Wilson prime of order n is a prime number p such that p2 exactly divides:
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.
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Wilson primes of order n step by step in the Julia programming language
This Julia code efficiently generates and prints Wilson primes within a specified limit. Wilson primes are prime numbers p
where (p-1)! ≡ -1 (mod p^2)
. The code leverages Julia's built-in Primes
module for fast prime number generation.
-
Function Definition:
wilsonprimes(limit = 11000)
defines a function that takes an optional limit as an argument and returns a list of Wilson primes within that limit.
-
Initialization:
- Initialize
sgn
to 1 andfacts
as the product of integers from 1 tolimit
, stored as a big integer. - Print a header for the output.
- Initialize
-
Prime Generation and Checking:
-
Iterate through values of
n
from 1 to 11:-
For each
n
, toggle the signsgn
to -1 or 1 to alternate the sign in the Wilson prime formula. -
Iterate through prime numbers
p
up to the limit:-
Check if
p
is greater thann
and if the congruence(facts[n < 2 ? 1 : n - 1] * facts[p - n] - sgn) % p^2 == 0
holds. This checks ifn
is a Wilson prime for the currentp
. -
If the congruence holds, print the prime
p
.
-
-
-
-
Output:
- After iterating through all values of
n
, the list of Wilson primes is printed for eachn
.
- After iterating through all values of
Source code in the julia programming language
using Primes
function wilsonprimes(limit = 11000)
sgn, facts = 1, accumulate(*, 1:limit, init = big"1")
println(" n: Wilson primes\n--------------------")
for n in 1:11
print(lpad(n, 2), ": ")
sgn = -sgn
for p in primes(limit)
if p > n && (facts[n < 2 ? 1 : n - 1] * facts[p - n] - sgn) % p^2 == 0
print("$p ")
end
end
println()
end
end
wilsonprimes()
You may also check:How to resolve the algorithm Water collected between towers step by step in the Scheme programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the Scala programming language
You may also check:How to resolve the algorithm LU decomposition step by step in the Rust programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the Forth programming language
You may also check:How to resolve the algorithm Even or odd step by step in the AArch64 Assembly programming language