AKS test in Javascript: Find if the number is prime or composite.

AKS test in Javascript: Find if the number is prime or composite.

AKS test stands for Agrawal–Kayal–Saxena primality test which is the first mathematical algorithm that can tell if a number is prime or composite.

AKS test - If the coefficients of this equation are divisible by 'p'(the number you want to test) then p is a prime number else it's composite. $$(x-1)^p-(x^p-1)$$

Now that definition is out of the way let's understand this in simpler words. We all have learnt the pascals triangle in high school. Pascals-triangle.png Now remove the first and last number of pascals triangle. Pascals-triangle-1.jpg AKS test says that if the elements in a row are divisible by its row number then it's a prime number Pascals-triangle-color-blue.jpg

Step 1: Find Pascal coefficients of the given number

Pascals formula $$\sum_{k=1}^{n}\frac{n!}{k!(n-k)!)}$$ Before writing a function for pascal coefficients, let's write a function for finding factorial. img/factorial.png Function to find pascals coefficients pascalsCoefficient.png

Step 2: Write a function to find if the number is prime or composite

  1. every function returns true if logic holds true for every element of the array.
  2. pop() and shift() removes the first and last element in pascals coefficient verifyPrime.png Full Code

    Reference

  3. Pascal's diagram was taken from.
  4. Code snippets were made using
  5. Illustration in cover photo