How to resolve the algorithm Wilson primes of order n step by step in the Mathematica/Wolfram Language programming language
How to resolve the algorithm Wilson primes of order n step by step in the Mathematica/Wolfram Language 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 Mathematica/Wolfram Language programming language
Purpose of the Code:
This code finds Wilson primes up to a certain limit.
Explanation:
1. Defining the WilsonPrime Function:
ClearAll[WilsonPrime]
WilsonPrime[n_Integer] := Module[{primes, out},
This defines a function named WilsonPrime
that takes an integer n
as input and returns a Wilson prime less than or equal to n
.
2. Generating a List of Primes:
primes = Prime[Range[PrimePi[11000]]];
This generates a list of all prime numbers less than or equal to 11,000 using the Prime
and Range
functions.
3. Calculating Wilson Prime Candidates:
out = Reap@Do[
If[Divisible[((n - 1)!) ((p - n)!) - (-1)^n, p^2], Sow[p]]
,
{p, primes}
];
This is the core of the Wilson prime calculation. It iterates through the primes in the primes
list and performs the following checks:
- Calculates
((n - 1)!) ((p - n)!) - (-1)^n
. - Checks if the result is divisible by
p^2
. - If it is divisible, it "sows" the prime
p
into theout
list.
4. Extracting the Wilson Prime:
First[out[[2]], {}]
After the loop has completed, the code extracts the first element from the second part (the sown elements) of out
. This is the smallest Wilson prime less than or equal to n
.
5. Looping Over Integers:
Do[
Print[WilsonPrime[n]]
,
{n, 1, 11}
]
Finally, this loop iterates over the integers from 1 to 11 and prints the Wilson prime for each value of n
using the WilsonPrime
function.
Example Output:
For n
from 1 to 11, the output will be:
5
13
563
1279
2207
3479
8257
12053
17207
19609
23309
Source code in the wolfram programming language
ClearAll[WilsonPrime]
WilsonPrime[n_Integer] := Module[{primes, out},
primes = Prime[Range[PrimePi[11000]]];
out = Reap@Do[
If[Divisible[((n - 1)!) ((p - n)!) - (-1)^n, p^2], Sow[p]]
,
{p, primes}
];
First[out[[2]], {}]
]
Do[
Print[WilsonPrime[n]]
,
{n, 1, 11}
]
You may also check:How to resolve the algorithm Shell one-liner step by step in the C programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the bc programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Elixir programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Erlang programming language