How to resolve the algorithm Zsigmondy numbers step by step in the Mathematica/Wolfram Language programming language
How to resolve the algorithm Zsigmondy numbers step by step in the Mathematica/Wolfram Language programming language
Table of Contents
Problem Statement
Zsigmondy numbers n to a, b, are the greatest divisor of an - bn that is coprime to am - bm for all positive integers m < n.
Suppose we set a = 2 and b = 1. (Zs(n,2,1)) For each n, find the divisors of an - bn and return the largest that is coprime to all of am - bm, where m is each of the positive integers 1 to n - 1. When n = 4, 24 - 14 = 15. The divisors of 15 are 1, 3, 5, and 15. For m = 1, 2, 3 we get 2-1, 22-12, 23-13, or 1, 3, 7. The divisors of 15 that are coprime to each are 5 and 1, (1 is always included). The largest coprime divisor is 5, so Zs(4,2,1) = 5.
When n = 6, 26 - 16 = 63; its divisors are 1, 3, 7, 9, 21, 63. The largest divisor coprime to all of 1, 3, 7, 15, 31 is 1, (1 is always included), so Zs(6,2,1) = 1.
If a particular an - bn is prime, then Zs(n,a,b) will be equal to that prime. 25 - 15 = 31 so Zs(5,2,1) = 31.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Zsigmondy numbers step by step in the Mathematica/Wolfram Language programming language
Function Definition
ClearAll[zsigmondy]
Attributes[zsigmondy] = Listable;
zsigmondy[a_Integer, b_Integer, 1] := a - b /; a >= b;
This defines a function zsigmondy
that calculates the Zsigmondy function for the given integers a
and b
with an exponent n
. It uses the Listable
attribute to allow the function to operate element-wise on lists of input values.
The function checks if a
is greater than or equal to b
and if so, it returns the difference between a
and b
. This is the base case for the recursive calculation.
Recursive Definition
zsigmondy[a_Integer, b_Integer, n_Integer] := (
hatvanyok = a^Range[n] - b^Range[n];
kishatvany = a^Range[n - 1] - b^Range[n - 1];
biggestelement = Part[hatvanyok, n];
divisors = Divisors[biggestelement];
divisorpairs = Join @@ (Thread[{#, kishatvany}] & /@ divisors);
coprimepairs = Select[divisorpairs, CoprimeQ[#[[1]], #[[2]]] &];
firstelement = coprimepairs[[All, 1]];
revesedelements = ReverseSort[firstelement];
Commonest[revesedelements, 1]) /; a >= b
The recursive definition handles the case where n
is greater than 1.
hatvanyok
is a list of the powers ofa
andb
up to the exponentn
.kishatvany
is a list of the powers ofa
andb
up to the exponentn-1
.biggestelement
is the last element ofhatvanyok
.divisors
is a list of the divisors ofbiggestelement
.divisorpairs
is a list of pairs of divisors and their corresponding elements inkishatvany
.coprimepairs
is a list of divisor pairs where the first and second elements are coprime (have no common factors).firstelement
is a list of the first elements of the coprime pairs.revesedelements
is a list of the first elements infirstelement
sorted in reverse order.Commonest[revesedelements, 1]
returns the most common element inrevesedelements
.
This recursive definition essentially finds the most common divisor of a^n - b^n
and a^(n-1) - b^(n-1)
among all possible divisor pairs where the first and second elements are coprime.
Data Generation
data2 = MapThread[
zsigmondy, {{2, 3, 4, 5, 6, 7, 3, 5, 7, 7}, {1, 1, 1, 1, 1, 1, 2,
3, 3, 5}, {Range[10], Range[10], Range[10], Range[10], Range[10],
Range[10], Range[10], Range[10], Range[10], Range[10]}}];
This generates data for the Zsigmondy function for various pairs of integers and exponents.
data1 = List["Zsigmondy:2,1,n", "Zsigmondy:3,1,n", "Zsigmondy:4,1,n",
"Zsigmondy:5,1,n", "Zsigmondy:6,1,n", "Zsigmondy:7,1,n",
"Zsigmondy:3,2,n", "Zsigmondy:5,3,n", "Zsigmondy:7,3,n",
"Zsigmondy:7,5,n"];
This defines a list of labels for the different pairs of integers and exponents.
Grid Display
Grid[Transpose@{data1, data2}~
Prepend~{"pair of numbers", "Zsigmondy numbers"},
Dividers -> {All, {1 -> True, 2 -> True}}]
This creates a grid display that shows the labels and the corresponding Zsigmondy numbers. The grid is divided into columns for the pairs of numbers and the Zsigmondy numbers, and rows for each pair of integers and exponent. The first and second rows of the grid contain the labels and the Zsigmondy numbers, respectively.
Source code in the wolfram programming language
ClearAll[zsigmondy]
Attributes[zsigmondy] = Listable;
zsigmondy[a_Integer, b_Integer, 1] := a - b /; a >= b;
zsigmondy[a_Integer, b_Integer, n_Integer] := (
hatvanyok = a^Range[n] - b^Range[n];
kishatvany = a^Range[n - 1] - b^Range[n - 1];
biggestelement = Part[hatvanyok, n];
divisors = Divisors[biggestelement];
divisorpairs = Join @@ (Thread[{#, kishatvany}] & /@ divisors);
coprimepairs = Select[divisorpairs, CoprimeQ[#[[1]], #[[2]]] &];
firstelement = coprimepairs[[All, 1]];
revesedelements = ReverseSort[firstelement];
Commonest[revesedelements, 1]) /; a >= b
data2 = MapThread[
zsigmondy, {{2, 3, 4, 5, 6, 7, 3, 5, 7, 7}, {1, 1, 1, 1, 1, 1, 2,
3, 3, 5}, {Range[10], Range[10], Range[10], Range[10], Range[10],
Range[10], Range[10], Range[10], Range[10], Range[10]}}];
data1 = List["Zsigmondy:2,1,n", "Zsigmondy:3,1,n", "Zsigmondy:4,1,n",
"Zsigmondy:5,1,n", "Zsigmondy:6,1,n", "Zsigmondy:7,1,n",
"Zsigmondy:3,2,n", "Zsigmondy:5,3,n", "Zsigmondy:7,3,n",
"Zsigmondy:7,5,n"];
Grid[Transpose@{data1, data2}~
Prepend~{"pair of numbers", "Zsigmondy numbers"},
Dividers -> {All, {1 -> True, 2 -> True}}]
pair of numbers Zsigmondy numbers
Zsigmondy:2,1,n {1, {3}, {7}, {5}, {31}, {1}, {127}, {17}, {73}, {11}}
Zsigmondy:3,1,n {2, {1}, {13}, {5}, {121}, {7}, {1093}, {41}, {757}, {61}}
Zsigmondy:4,1,n {3, {5}, {7}, {17}, {341}, {13}, {5461}, {257}, {1387}, {41}}
Zsigmondy:5,1,n {4, {3}, {31}, {13}, {781}, {7}, {19531}, {313}, {15751}, {521}}
Zsigmondy:6,1,n {5, {7}, {43}, {37}, {311}, {31}, {55987}, {1297}, {46873}, {1111}}
Zsigmondy:7,1,n {6, {1}, {19}, {25}, {2801}, {43}, {137257}, {1201}, {39331}, {2101}}
Zsigmondy:3,2,n {1, {5}, {19}, {13}, {211}, {7}, {2059}, {97}, {1009}, {11}}
Zsigmondy:5,3,n {2, {1}, {49}, {17}, {1441}, {19}, {37969}, {353}, {19729}, {421}}
Zsigmondy:7,3,n {4, {5}, {79}, {29}, {4141}, {37}, {205339}, {1241}, {127639}, {341}}
Zsigmondy:7,5,n {2, {3}, {109}, {37}, {6841}, {13}, {372709}, {1513}, {176149}, {1661}}
You may also check:How to resolve the algorithm Handle a signal step by step in the Crystal programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Menu step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Box the compass step by step in the Action! programming language