$a^2-b^2 = x$ where $a,b,x$ are natural numbers

Suppose that $a^2-b^2 =x$ where $a,b,x$ are natural numbers.

Suppose $x$ is fixed. If there is one $(a,b)$ found, can there be another $(a,b)$?

Also, would there be a way to know how many such $(a,b)$ exists?


Note that $a^2-b^2=(a+b)(a-b)$ and that $(a+b)=c$ and $(a-b)=d$ are either both odd or both even.

If $x=cd$ where $c>d$ and $c$ and $d$ have the same parity then $a=\frac {c+d}2, b=\frac {c-d}2$ are natural numbers which work for $x$.

It is always possible to express an odd number $x$ in this way, using $c=x, d=1$. If $x$ is prime there is no other factorisation.

If $x$ is even, it must be divisible by 4, and if $x=4y$ then $c=2y, d=2$ will do provided $y>1$.

The number of solutions depends on the number of factorisations into factors of like parity.


You want $x = a^2 - b^2 = (a-b)(a+b)$. Let $m = a-b$ and $n = a+b$, then note that $a = (m+n)/2$ and $b = (n-m)/2$. For these to be natural numbers, you want both $m$ and $n$ to be of the same parity (i.e., both odd or both even), and $m \le n$. For any factorization $x = mn$ satisfying these properties, $a = (m+n)/2$ and $b = (n-m)/2$ will be a solution.

The answer to your question of how many such $(a,b)$ exist is therefore the same as how many ways there are of writing $x = mn$ with both factors of the same parity and $m \le n$. Let $d(x)$ denote the number of divisors of $x$. (For instance, $d(12) = 6$ as $12$ has $6$ factors $1, 2, 3, 4, 6, 12$.)

  • If $x$ is odd, note that for any divisor $m$ of $x$, the factorization $x = mn$ (where $n = x/m$) has both factors odd. Also, out of any two factorizations $(m,n)$ and $(n,m)$, one of them will have $m < n$ and the other will have $m > n$, so the number of "good" factorizations of $x$ is $d(x)/2$. In the case where $x$ is a perfect square, this means either $\lceil d(x)/2 \rceil$ or $\lfloor d(x)/2 \rfloor$ depending on whether or not you want to allow the solution $a = \sqrt{x}$, $b = 0$.

  • If $x$ is even, then $m$ and $n$ can't be both odd so they must be both even, so $x$ must be divisible by $4$. Say $x = 2^k \cdot l$, where $k \ge 2$. How many factorizations $x = mn$ are there with both $m$ and $n$ being even? Well, there are $d(x)$ factorisations of $x$ as $x = mn$. Of these, the factorisations in which all the powers of $2$ go on one side can be got by taking any of the $d(l)$ factorisations of $l$, and then putting the powers of two entirely on one of the $2$ sides. So the number of representations $x = mn$ with $m$ and $n$ both being even is $d(x) - 2d(l)$. Again, the number where $m \le n$ is half that, namely $(d(x) - 2d(l))/2$, where in the case $x$ is a perfect square, you mean either $\lceil (d(x) - 2d(l))/2 \rceil$ or $\lfloor (d(x) - 2d(l))/2 \rfloor$ depending on whether you want to allow the $b = 0$ solution or not.


Here is a program using Sage that can print all solutions $(a,b)$ for any given $x$.

#!/path/to/sage

print "Hello"
def mn(x):
  '''Returns all (m,n) such that x = mn, m <= n, and both odd/even'''
  for m in divisors(x):
    n = x/m
    if m <= n and (m % 2 == n % 2): yield (m,n)
    elif m > n: break
def ab(x):
  '''Returns all (a,b) such that a^2 - b^2 = x'''
  return [((m+n)/2, (n-m)/2) for (m,n) in mn(x)]

def num_ab(x):
  '''The number of (a,b) such that a^2 - b^2 = x'''
  dx = number_of_divisors(x)
  if x % 2: return ceil(dx / 2)
  l = odd_part(x)
  dl = number_of_divisors(l)
  return ceil((dx - 2*dl) / 2)

# Do it in two ways to check that we have things right
for x in range(1,1000): assert num_ab(x) == len(ab(x))
# Some examples
print ab(12)
print ab(100)
print ab(23)
print ab(42)
print ab(999)
print ab(100000001)

It prints:

Hello
[(4, 2)]
[(26, 24), (10, 0)]
[(12, 11)]
[]
[(500, 499), (168, 165), (60, 51), (32, 5)]
[(50000001, 50000000), (2941185, 2941168)]

and you can verify that, for instance, $168^2 -165^2 = 999$.