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. Now remove the first and last number of pascals triangle. AKS test says that if the elements in a row are divisible by its row number then it's a prime number
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. Function to find pascals coefficients
Step 2: Write a function to find if the number is prime or composite
every
function returns true if logic holds true for every element of the array.pop()
andshift()
removes the first and last element in pascals coefficient Full CodeReference
- Pascal's diagram was taken from.
- Code snippets were made using
- Illustration in cover photo