How to resolve the algorithm AKS test for primes step by step in the Mathematica / Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm AKS test for primes step by step in the Mathematica / Wolfram Language programming language

Table of Contents

Problem Statement

The AKS algorithm for testing whether a number is prime is a polynomial-time algorithm based on an elementary theorem about Pascal triangles. The theorem on which the test is based can be stated as follows: are divisible by

p

{\displaystyle p}

.

Using

p

3

{\displaystyle p=3}

:

And all the coefficients are divisible by 3,   so 3 is prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm AKS test for primes step by step in the Mathematica / Wolfram Language programming language

Explanation:

Part 1: Powers of (x-1)

  • Print["powers of (x-1)"]: Prints the title "powers of (x-1)".
  • (x - 1)^( Range[0, 7]): Calculates the powers of (x-1) from 0 to 7 and stores the results in a list.
  • Expand simplifies the expressions.
  • TableForm: Displays the results in a tabular format.

Part 2: Primes under 50

  • poly[p_] := (x - 1)^p - (x^p - 1) // Expand;: Defines a function poly that calculates the polynomial (x - 1)^p - (x^p - 1) for a given integer p.
  • coefflist[p_Integer] := Coefficient[poly[p], x, #] & /@ Range[0, p - 1];: Defines a function coefflist that extracts the coefficients of the polynomial poly for a given p.
  • AKSPrimeQ[p_Integer] := (Mod[coefflist[p] , p] // Union) == {0};: Defines a function AKSPrimeQ that checks if a given integer p is prime. It does this by calculating the coefficients of the polynomial poly for p and checking if all non-zero coefficients are divisible by p. If they are, then p is prime.
  • Select[Range[1, 50], AKSPrimeQ]: Uses the function Select to find all primes under 50.

Source code in the wolfram programming language

Print["powers of (x-1)"]
(x - 1)^( Range[0, 7]) // Expand // TableForm   
Print["primes under 50"]
poly[p_] := (x - 1)^p - (x^p - 1) // Expand;
coefflist[p_Integer] := Coefficient[poly[p], x, #] & /@ Range[0, p - 1];
AKSPrimeQ[p_Integer] := (Mod[coefflist[p] , p] // Union) == {0};
Select[Range[1, 50], AKSPrimeQ]


  

You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the True BASIC programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Bell numbers step by step in the REXX programming language