RosettaCodeData/Task/AKS-test-for-primes/00-TASK.txt

39 lines
2.2 KiB
Plaintext

The [http://www.cse.iitk.ac.in/users/manindra/algebra/primality_v6.pdf 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:
* &nbsp; a number &nbsp; <big><big><math>p</math></big></big> &nbsp; is prime &nbsp; if and only if &nbsp; all the coefficients of the polynomial expansion of
::: <big><big><math>(x-1)^p - (x^p - 1)</math></big></big>
are divisible by &nbsp; <big><big><math>p</math>.</big></big>
;Example:
Using &nbsp; <big><big><math>p=3</math>:</big></big>
<big><big>(x-1)^3 - (x^3 - 1)
= (x^3 - 3x^2 + 3x - 1) - (x^3 - 1)
= -3x^2 + 3x</big></big>
And all the coefficients are divisible by '''3''', &nbsp; so '''3''' is prime.
{{alertbox|#ffe4e4|'''Note:'''<br/>This task is '''not''' the AKS primality test. &nbsp; 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 &nbsp; <big><big><math>p</math></big></big> &nbsp; generates the coefficients of the expanded polynomial representation of &nbsp; <big><big><math>(x-1)^p</math>.</big></big>
# Use the function to show here the polynomial expansions of &nbsp; <big><big><math>(x-1)^p</math></big></big> &nbsp; for &nbsp; <big><big><math>p</math></big></big> &nbsp; in the range &nbsp; '''0''' &nbsp; to at least &nbsp; '''7''', &nbsp; inclusive.
# Use the previous function in creating another function that when given &nbsp; <big><big><math>p</math></big></big> &nbsp; returns whether &nbsp; <big><big><math>p</math></big></big> &nbsp; is prime using the theorem.
# Use your test to generate a list of all primes ''under'' &nbsp; '''35'''.
# '''As a stretch goal''', &nbsp; generate all primes under &nbsp; '''50''' &nbsp; (needs integers larger than 31-bit).
;References:
* [https://en.wikipedia.org/wiki/AKS_primality_test Agrawal-Kayal-Saxena (AKS) primality test] (Wikipedia)
* [http://www.youtube.com/watch?v=HvMSRWTE2mI Fool-Proof Test for Primes] - Numberphile (Video). The accuracy of this video is disputed -- at best it is an oversimplification.
<br><br>