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:

   a number   



p


{\displaystyle p}

   is prime   if and only if   all the coefficients of the polynomial expansion of
 



(
x
−
1

)

p


−
(

x

p


−
1
)


{\displaystyle (x-1)^{p}-(x^{p}-1)}


are divisible by   



p


{\displaystyle p}

.



Example

Using   



p
=
3


{\displaystyle p=3}

:

         (x-1)^3 - (x^3 - 1)
            = (x^3 - 3x^2 + 3x - 1) - (x^3 - 1)
            = -3x^2 + 3x



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





 Note:
This task is not the AKS primality test.   It is an inefficient exponential time algorithm discovered in the late 1600s and used as an introductory lemma in the AKS derivation.



Task



 Create a function/subroutine/method that given   



p


{\displaystyle p}

   generates the coefficients of the expanded polynomial representation of   



(
x
−
1

)

p




{\displaystyle (x-1)^{p}}

.
 Use the function to show here the polynomial expansions of   



(
x
−
1

)

p




{\displaystyle (x-1)^{p}}

   for   



p


{\displaystyle p}

   in the range   0   to at least   7,   inclusive.
 Use the previous function in creating another function that when given   



p


{\displaystyle p}

   returns whether   



p


{\displaystyle p}

   is prime using the theorem.
 Use your test to generate a list of all primes under   35.
 As a stretch goal,   generate all primes under   50   (needs integers larger than 31-bit).


References

 Agrawal-Kayal-Saxena (AKS) primality test (Wikipedia) 
 Fool-Proof Test for Primes - Numberphile (Video). The accuracy of this video is disputed -- at best it is an oversimplification.


 
from math import binom
import strutils
 
# Table of unicode superscript characters.
const Exponents: array[0..9, string] = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"]
 
iterator coeffs(n: int): int =
  ## Yield the coefficients of the expansion of (x - 1)ⁿ.
  var sign = 1
  for k in 0..n:
    yield binom(n, k) * sign
    sign = -sign
 
iterator polyExpansion(n: int): tuple[c, e: int] =
  ## Yield the coefficients and the exponents of the expansion of (x - 1)ⁿ.
  var e = n
  for c in coeffs(n):
    yield(c, e)
    dec e
 
proc termString(c, e: int): string =
  ## Return the string for the term c * e^n.
  if e == 0:
    result.addInt(c)
  else:
    if c != 1:
      result.addInt(c)
    result.add('x')
    if e != 1:
      result.add(Exponents[e])
 
proc polyString(n: int): string =
  ## Return the string for the expansion of (x - 1)ⁿ.
  for (c, e) in polyExpansion(n):
    if c < 0:
      result.add(" - ")
    elif e != n:
      result.add(" + ")
    result.add(termString(abs(c), e))
 
proc isPrime(n: int): bool =
  ## Check if a number is prime using the polynome expansion.
  result = true
  for (c, e) in polyExpansion(n):
    if e in 1..(n-1):   # xⁿ and 1 are eliminated by the subtraction.
      if c mod n != 0:
        return false
 
#---------------------------------------------------------------------------------------------------
 
echo "Polynome expansions:"
for p in 0..9:
  echo "(x - 1)$1 = $2".format(Exponents[p], polyString(p))
 
var primes: string
for p in 2..34:
  if p.isPrime():
    primes.addSep(", ", 0)
    primes.addInt(p)
echo "\nPrimes under 35: ", primes
 
for p in 35..50:
  if p.isPrime():
    primes.add(", ")
    primes.addInt(p)
echo "\nPrimes under 50: ", primes
 