algorithmmathcomputer-scienceprimesp-np

Primes in P - what about running till the sqrt?


I'm learning about P and NP.
I've read that the problem of deciding if a given number is prime is a problem in P, which means that it has a polynomial-time algorithm which solves it.
I've also read that this fact was proven in 2002, with the algorithm of AKS.

As we all know, we can decide if a particular number is prime by running till its square root.

pseudo-code:

isPrime(N):

sqrt(N) <- squareRoot(N)

for i from 2 to Sqrt(N)
    if (n mod i == 0)
        return false
return true

My question is simple:
Why the algorithm above doesn't prove that this problem is in P?
Thanks :)


Solution

  • Whether an algorithm is polynomial-time depends on the representation of the input. For example, the large number 9223372036854775807 fits in a 64-bit unsigned type. The significance of the AKS result is that it's polynomial in the number of bits (e.g., 64) rather than the number itself (e.g., 9223372036854775807).