# Can this primality test be optimized to runs in polynomial time?

Theorem :

Let $k$ be positive integer such that : $k \in [1,\lfloor \sqrt p \rfloor ]$

$\binom{p}{k} \equiv 0 \pmod p$ for all $k$ iff $p$ is a prime number .

Test written in Maple :

Test is general , unconditional , deterministic but it runs in exponential time .

Can it be optimized to runs in polynomial time ?

-
No.${}{}{}{}{}$ – TonyK Jan 20 '12 at 10:36
Given how long it took to prove that primality testing was in P, and how much more complicated AKS primality testing is than this, I highly doubt it. – Peter Taylor Jan 20 '12 at 10:56
What's the usage of this wired primality testing? You can use fast primality testing with miller rabin, and have a good chance. you can see my sample C# code in SO. – Saeed Jan 20 '12 at 11:34
Currently I see AKS theorem, if you want exact algorithm they solve it in their paper, and is not easy as miller rabin test. In fact you tryed first part of their algorithm, but still it's slower than miller rabin. – Saeed Jan 20 '12 at 12:43

As a first optimization you could keep the current value of the binomial each time and update it with a multiplication and a division rather than recomputing at each step.

As a next optimization you might realize that each division is exact, so you can use Jebelean-Krandick bidirectional division.

Then you might realize that each step essentially tests if k divides p, and so replace the binomial calculation with a modulus test.

Then you could run the test over only the primes k up to $\sqrt p$.

So after all these optimizations, you are left with trial division, which runs in exponential time.

-

You can use dynamic programming I mean instead of compute binomial(p,k) in each loop use this one : temp := temp * (p - k+1) / k;. and at the start of the program set temp to 1

I used this fact : binomial(p,k) = binomial(p,k-1) * (p-k+1) / k.

-
But that doesn't help, because performing $O(1)$ work for each of $\sqrt{n}$ candidates is already exponential time, as $\sqrt{n}$ is exponential in $\log(n)$, which is the input size. – Peter Taylor Jan 20 '12 at 10:54
@PeterTaylor pedja want : "Can it be optimized to runs in polynomial time ?" so it works because O(√n) is better that polynomial! – Lrrr Jan 20 '12 at 10:57
No, Ali, as Peter said, $\sqrt n$ is exponential in the input size, which is $\log n$. Otherwise, you could just do trial division up to $\sqrt n$ and tell everyone you have a polynomial time factorization algorithm and become fabulously wealthy. – Gerry Myerson Jan 20 '12 at 11:03
@Ali, yes, I can see the difference between $p!$ and $\sqrt n$. Can you see that $\sqrt n$ is not better than polynomial time, but much, much worse than polynomial time? – Gerry Myerson Jan 20 '12 at 11:54
Since the discussion got a bit off topic and more agressive, lets get back to the original point: The optimization proposed in the answer optimizes the runtime from $k!$ to $\sqrt{k}$, which is awesome! But: time and space complexity are measured in input length, and input length $n = \log k$, so the time complexity of the algorithm is $\sqrt{k} = \sqrt{2^n}$. Therefore the optimized version has the time complexity $O(\sqrt{2^n})$ which is not polynomial. Now please be nice to eachother :) – Mike B. Jan 20 '12 at 14:17