How to resolve the algorithm AKS test for primes step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm AKS test for primes step by step in the Haskell 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 Haskell programming language

The provided Haskell code implements several functions related to number theory, specifically dealing with expanding polynomials and testing for prime numbers. Let's go through each function and explain its purpose:

  1. expand p: This function takes a positive integer p and returns a list of numbers formed by a polynomial expansion of the form (x-1)^p. It uses the scanl function to accumulate the polynomial expansion result by multiplying the previous value (z) by (p-i+1) and dividing by i, where i ranges from 1 to p.

  2. test p: This function tests if a given integer p is a prime number. It uses the expand function to get the polynomial expansion of (x-1)^p. A prime number is defined as a natural number greater than 1 that has no positive divisors other than 1 and itself. For p to be prime, the polynomial expansion (x-1)^p must not be divisible by any n between 2 and p-1. The test function checks this condition by attempting to divide n into all elements of the polynomial expansion except the first and last elements. If any of these divisions result in a remainder of 0, then p is not prime; otherwise, it is prime.

  3. printPoly: This function takes a list of numbers representing a polynomial and converts it into a string representation. It iterates through the elements of the list from the second-to-last to the first and constructs a string with the following format:

    • pow i: A string representing the power of x for the current element. If the power is greater than 1, it includes the power as a superscript; otherwise, it just prints "x".
    • sgn (l-i): A string representing the sign of the current element. If the element index i is even, it uses a plus sign '+'; otherwise, it uses a minus sign '-'.
    • show (p!!(i-1)): A string representation of the numerical value of the current element.
  4. main: This is the main function of the program. It performs the following actions:

    • Prints the polynomial expansions for (x-1)^p for p values from 0 to 10 and displays them in a human-readable format.
    • Generates a list of numbers from 1 to 100 and filters out the prime numbers using the test function. It then prints the list of prime numbers in the given range.

In summary, this Haskell code provides functions for expanding polynomials of the form (x-1)^p and testing if a given number is prime. It also includes a printPoly function for displaying polynomials in a readable format. The main function showcases these functions by generating polynomial expansions and identifying prime numbers within a given range.

The provided Haskell code defines several functions to work with polynomials. Here's a breakdown of the code:

expand Function:

The expand function generates a list of coefficients for the polynomial (x-1)^p, where p is a positive integer input parameter. It uses the formula for expanding this polynomial, which involves multiplying the previous coefficient by (p-i+1) and dividing by i for each value of i from 1 to p.

test Function:

The test function checks if a given integer p is a prime number. It does this by filtering out the coefficients of (x-1)^p and checking if any of them are divisible by p. If all coefficients are divisible by p, then p is not prime, and the function returns False. Otherwise, it returns True.

printPoly Function:

The printPoly function converts a list of polynomial coefficients into a string representation of the polynomial. It iterates over the list, starting from the second to last coefficient (the coefficients are indexed in a 0-based manner), and constructs a string representation of each term in the polynomial. It includes a sign ("+" or "-"), the power of x in the term (e.g., "x^2"), and the coefficient value.

Main Function:

The main function is the entry point of the program. It performs two tasks:

  1. It prints out the coefficients of the polynomial (x-1)^p for small values of p (0 to 10) and prints the resulting polynomial in string form.

  2. It filters out the prime numbers up to 100 using the test function and prints a list of these primes.

Source code in the haskell programming language

expand p = scanl (\z i -> z * (p-i+1) `div` i) 1 [1..p]


test p | p < 2     = False
       | otherwise = and [mod n p == 0 | n <- init . tail $ expand p]


printPoly [1] = "1"
printPoly p   = concat [ unwords [pow i, sgn (l-i), show (p!!(i-1))]
                       | i <- [l-1,l-2..1] ] where
    l = length p
    sgn i = if even i then "+" else "-"
    pow i = take i "x^" ++ if i > 1 then show i else ""


main = do
    putStrLn "-- p: (x-1)^p for small p"
    putStrLn $ unlines [show i ++ ": " ++ printPoly (expand i) | i <- [0..10]]
    putStrLn "-- Primes up to 100:"
    print (filter test [1..100])


  

You may also check:How to resolve the algorithm Water collected between towers step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Pi step by step in the Ada programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the zkl programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Swift programming language
You may also check:How to resolve the algorithm Entropy/Narcissist step by step in the Nim programming language