3

I am reading an article by Cook [1]. In it he writes:

The set of perfect squares is in P, since Newton's method can be used to efficiently approximate square roots.

I can see how to use Newton's method to decide whether a natural number is a perfect square or not, but it seems tricky to analyse.

Is there an easier way to see that the set of perfect squares is in P?


  1. S. Cook, The P versus NP problem, Online
Raphael
  • 72,336
  • 29
  • 179
  • 389
Wei-Cheng Liu
  • 305
  • 2
  • 14
  • 1
    Please restrict yourself to only one question per post. Since you have an answer to 1) and 2) is a duplicate, I'm editing to leave only 3). – Raphael May 27 '16 at 14:56
  • 1
    If you are interested in the analysis of your algorithm, please refer to the reference question and give it a try. Feel free to ask new questions about specific steps you have issues with. – Raphael May 27 '16 at 14:58

1 Answers1

13

A simple answer is "binary search". Keep track of a lower bound (starts out with 1) and an upper bound (starts out with $n$). In each iteration compute the midpoint $m$. In polytime check if $m∗m=n$. If so, terminate. Otherwise, if $m∗m>n$ reduce the upper bound to $m$, else increase the lower bound to $m$. If the upper bound and the lower bound become the same and the midpoint squared is not equal to $n$, then $n$ is not a perfect square. This terminates in $\log n$ steps, which is polynomial in the bit size of $n$, i.e., the length of the input. Each iteration runs in polynomial time as well, so the overall algorithm runs in polytime.

Denis Pankratov
  • 1,483
  • 10
  • 16