# Given $m,n$ positive integers, count how many positive integer pairs $(a,b)$ are there such that $a(m-a) = b(n-b)$.

Given positive integers $m,n$ is there a closed form formula for the number of pairs $(a,b)$ such that $1 \leq a \leq m, 1 \leq b \leq n$ and $a(m-a) = b(n-b)$? I wrote a R program to count these for various values of $m,n$ and apparently there is no pattern we can conjecture. For example, for $n=43$, for the values of $m \leq 12$, it is $0$ and for $13, 14, 15, \ldots$, the count is $4, 0, 0, 0, 4, 0, 0, 0, 0, \ldots$. Also for $m=41$ the count is 12, for $43$ it is 84 (when $m=n$, I have a good way to count), $m=47$, the count is 16. Since the equation is symmetric, when $m \neq n$, we can guess that the count is even.

• For $m=n$ the count is infinite as $a=b$ is a solution. As you gave a finite answer for that case your R program may not be exhaustive in other cases as well. – Ian Miller Aug 10 '17 at 4:16
• Thanks. I need only those $a,b$ such that $1 \leq a < m$ and $1 \leq b < n$. Edited the problem statement. – user348749 Aug 10 '17 at 4:19

Suppose $m>n$. Let $D$ be the set of pairs $(s,t)$ of integers satisfying the following conditions:
• $st=m^2-n^2$
• $|s|,|t|<m+n$.
• $s\equiv m+n\pmod2$
• $s+t\equiv2m\pmod4$
Then the number of such pairs $(a,b)$ is $|D|$. This is not a closed formula, but factorising $m-n$ and $m+n$ and enumerating the factors of $m^2-n^2$ should be faster than considering all pairs $(a,b)$ with $0<a<m$, $0<b<n$.
To see that $D$ is in bijection with the pairs $(a,b)$, note that $$(2a+2b-m-n)(2a-2b-m+n)=4a(a-m)+m^2-4b(b-n)-n^2$$ so the given equation is equivalent to $$(2a+2b-m-n)(2a-2b-m+n)=m^2-n^2.$$ The bijection is given by $$s=2a-2b-m+n,\,t=2a+2b-m-n,$$ $$a=(t+s+2m)/4,\,b=(t-s+2n)/4.$$ Note that the parity conditions on $(s,t)$ ensure that $(a,b)$ are integers. The inequalities on $s,t$ are equivalent to $0<a<m$ and $0<b<n$. Indeed $|2a-m|<m$ and $|2b-n|<n$ implies $|s|,|t|<m+n$. Conversely suppose $|s|,|t|<m+n$. Note that we have two symmetries $(s,t)\mapsto(t,s)$ and $(s,t)\mapsto(-s,-t)$ of $D$ which correspond to $(a,b)\mapsto(a,n-b)$ and $(a,b)\mapsto(m-a,n-b)$. Therefore we may suppose $0\leq|s|\leq t$. But $st=m^2-n^2$ implies $s,t$ have the same sign, so $0\leq s\leq t$. Now $t<m+n$ and $st=m^2-n^2$ implies $s>m-n$, so $$0<n/2\leq b<n.$$ Finally $a(m-a)=b(n-b)>0$ implies $0<a<m$.