Integer solutions to $c=a^2-b^2$ I have been working on the following problem:

For a given $c\in \Bbb Z^+$, find $a,b \in \Bbb Z^+: c=a^2-b^2$.

I have already figured out and proved a number of things:


*

*A way to directly determine if a particular instance is solvable:  $\exists a,b \in \Bbb Z^+: c=a^2-b^2 \iff c \not\equiv 2 \mod 4$

*An upper bound on the values for a (and b): $b \lt a\le\lfloor\frac{c-1}{2}\rfloor$

*And a simple algorithm to find a solution:

$$
\begin{align*}
 (a_0, b_0) &= (0, 0) \\
 (a_{n+1}, b_{n+1}) &= \begin{cases}
  (a_n+1, &b_n&)&\text{if } a_n^2-b_n^2 < c\\
  (a_n, &b_n+1&)&\text{if } a_n^2-b_n^2 > c\\
 \end{cases}
\end{align*}
$$

This can be iterated until a solution is found or the upper bound is reached.
I am currently trying to work out a more direct way to find an answer, but I have been unable to figure out any other ways to approach the problem.
How can I more directly find values for $a$ and $b$ for a given value of $c$?
 A: $$c = a^2 - b^2 = (a - b)(a + b) = X \cdot Y$$
which implies
$$ a = \frac{Y + X} 2$$
$$ b = \frac{Y - X} 2$$
which implies
$$X \equiv Y \pmod 2$$
which is why you get that $X\cdot Y \not \equiv 2 \pmod 4$ by just enumerating the possible values for $X$ and $Y$.
So to find a solution $(a,b)$ given $c$, first factor $c$.  Then:

Foreach $X|c$

Y = $\frac c X$
      If $X < Y \land X \equiv Y \pmod 2$ Then

$ a = \frac{Y + X} 2$
         $ b = \frac{Y - X} 2$



A: If $c=a^2-b^2$, then clearly $a>b$ otherwise $c$ would be negative, so we can write $a=b+k$
Where $k$ is also a positive integer,
Thus we want solutions to $(b+k,b)$ to $c=(b+k)^2-b^2=k(2b+k)$
Now if divide both sides by $k$ we get $\frac{c}{k}=(2b+k)$, so clearly $k|c$
Now subtracting $k$ we get $\frac{c}{k}-k=2b$
Thus all solutions to $(a,b)$ to $c=a^2-b^2$ are given by
$$(a,b)=(\frac{c}{2d}+\frac{d}{2},\frac{c}{2d}-\frac{d}{2})$$
Where $d<\sqrt{c}$ is a divisor of $c$ with $\frac{c}{d}\equiv d \text{ mod 2}$
So once you have run through all such divisors, you will have all the solutions.
