1

I am trying to implement an efficient algorithm which must determine whether for a given integer $n>0$ another integer $y$ exist such that $n+y^2$ is a perfect square.

Based on some properties of $n$, can we make a statement about the existence of such a $y$ efficiently? Or reverse, is it possible for this $n$ to determine that there will be definitely no such $y$?

What I found out so far:

I found in the literature that a diophantine equation of the form:

$$x^2-Dy^2=n$$

is a so-called "Norm-Form Equation". Hence in our case, we have the special case $D=1$. However, as simple as the equation looks, it does not appear to be. For example Richard A. Mollin involves in his Theorem 6.2.2 the divisor function which is not a very efficient operation:

enter image description here

It wold be great if there exist an efficient way to determine wheher for an positive integer $n$ exist another integer $y$ such that $n+y^2$ is a perfect square.

Additional note: Even if the check routine generates some false positives in favor of efficiency, that would be absolutely ok. Being able to rule out that for a given natural $n$, no such $y$ exists would also be extremely helpful.

  • 4
    Given $n$, you want to know if there exists a $y$ and an $x$ such that $n + y^2 = x^2$, or what is the same thing, $n = x^2 - y^2 = (x+y)(x-y)$. If $n$ is odd, the answer is always "yes": set $x = \frac{1}{2}(n+1)$ and $y = \frac{1}{2}(n-1)$. – Eric Towers Jan 26 '22 at 21:58
  • 4
    If $n$ is divisible by $4$, the answer is always "yes": set $x = \frac{1}{4}(n+4)$ and $y = \frac{1}{4}(n-4)$. – Eric Towers Jan 26 '22 at 22:02
  • 3
    How is checking whether $n$ has the remainder $2$ from division by $4$ "not very efficient"? Are you familiar with the division algorithm? – Conifold Jan 26 '22 at 22:12
  • 1
    ...The answer is: check the last two digits of $n$. If it divides by four the remainder is two, there is no such $y$. – JetfiRex Jan 26 '22 at 22:53
  • @EldarSultanow You forgot to mention that in our task we need ALL such y till some limit, not just single possible y. And also it would be worth to mention that most of times our n is also a square i.e. n == m^2, and we need to solve both tasks - at least to solve case when n is a square, and then also a general task when n is any integer. – Arty Jan 27 '22 at 04:08
  • Thank you all for these useful hints. – Eldar Sultanow Jan 27 '22 at 05:52

1 Answers1

4

Summarizing the comments; your question can be rephrased as:

Can we efficiently determine whether a given positive integer $n$ is of the form $n=x^2-y^2$, for some integer $x$ and $y$?

The answer is yes; a positive integer $n$ is of this form if and only if $n\not\equiv2\pmod{4}$. This has been asked here before, e.g. this question.