How many pairs of integers have a fixed GCD? Let $k \ge 0, a \ge 0$ be a fixed integer and $f(x,k, a)$ be the number of positive integers pairs $(m,n), m &lt; n \le x$ such that $\gcd(km-n, m + kn) = a$.

Question: Is there a closed form of expression $\lim_{x \to \infty}\dfrac{f(x,k,a)}{x^2}$ in terms of $a$ and $k$?

My experimental data suggest that a closed form exists.

$$
f(x,0,1) \sim 3 \pi^2 x^2
\\
f(x,1,1) \sim 2 \pi^2 x^2
\\
f(x,2,1) \sim \frac{5}{2} \pi^2 x^2
\\
f(x,3,1) \sim \frac{5}{3} \pi^2 x^2
\\
f(x,4,1) \sim \frac{17}{6} \pi^2 x^2
\\
f(x,5,1) \sim \frac{13}{7} \pi^2 x^2
\\
f(x,2,3) \sim \frac{5}{18} \pi^2 x^2
\\
f(x,1,5) \sim \frac{2}{25} \pi^2 x^2
$$

Code

s = 2
j = 0
a = 3
test = 2

target = set = 10^2
while True:
    r = 1
    while r &lt; s:
        if gcd(a*s - r,s + a*r) == test: 
            j = j + 1
        r = r + 1
    if s &gt; target:
        target = target + set
        print s, j, (pi^2*j/s^2).n()
    s = s + 1
$f(n,1,1) = \sum_{m &lt; n, \gcd(m-n,m+n) = 1} 1 = \sum_{m &lt; n} \sum_{d \mid m-n, d \mid m+n} \mu(d) = \sum_{d \mid 2n} \mu(d)\sum_{m &lt; n, m \equiv n \mod d} 1 = \sum_{d \mid 2n} \mu(d)\left[\frac{n}{d}+O(1)\right] = n\sum_{d \mid 2n} \frac{\mu(d)}{d}+O(\tau(2n))$.

So, $\sum_{n \le x} f(n,1,1) = \sum_{n \le x} \left[n\sum_{d \mid 2n} \frac{\mu(d)}{d}+O(n^{1/2})\right] = \sum_{d \le 2x} \frac{\mu(d)}{d}\sum_{n \le x, d \mid 2n} n + O(x^{3/2})$. Ignore the $O(x^{3/2})$. We get $\sum_{d \le 2x, d \equiv 1 \pmod{2}} \frac{\mu(d)}{d}\left[\frac{1}{2}\frac{x^2}{d}+O(x)\right]+\sum_{d \le 2x, d \equiv 0 \pmod{2}} \frac{\mu(d)}{d}\left[\frac{x^2}{d}+O(x)\right]$. Similarly, we can ignore the $O(x)$ terms, so we end up with $\frac{1}{2}x^2\sum_{d \le 2x} \frac{\mu(d)}{d^2}+\frac{1}{2}x^2\sum_{d \le 2x, d \equiv 0 \pmod{2}} \frac{\mu(d)}{d^2}$. The first term is basically $\frac{1}{2}x^2\sum_{d=1}^\infty \frac{\mu(d)}{d^2} = \frac{3}{\pi^2} x^2$. The second term can be figured out also. So that's how the $\pi^2$'s come about.

One can do $f(n,a,k)$ similarly.
$f(n,1,1) = \sum_{m &lt; n, \gcd(m-n,m+n) = 1} 1 = \sum_{m &lt; n} \sum_{d \mid m-n, d \mid m+n} \mu(d) = \sum_{d \mid 2n} \mu(d)\sum_{m &lt; n, m \equiv n \mod d} 1 = \sum_{d \mid 2n} \mu(d)\left[\frac{n}{d}+O(1)\right] = n\sum_{d \mid 2n} \frac{\mu(d)}{d}+O(\tau(2n))$.

So, $\sum_{n \le x} f(n,1,1) = \sum_{n \le x} \left[n\sum_{d \mid 2n} \frac{\mu(d)}{d}+O(n^{1/2})\right] = \sum_{d \le 2x} \frac{\mu(d)}{d}\sum_{n \le x, d \mid 2n} n + O(x^{3/2})$. Ignore the $O(x^{3/2})$. We get $\sum_{d \le 2x, d \equiv 1 \pmod{2}} \frac{\mu(d)}{d}\left[\frac{1}{2}\frac{x^2}{d}+O(x)\right]+\sum_{d \le 2x, d \equiv 0 \pmod{2}} \frac{\mu(d)}{d}\left[\frac{x^2}{d}+O(x)\right]$. Similarly, we can ignore the $O(x)$ terms, so we end up with $\frac{1}{2}x^2\sum_{d \le 2x} \frac{\mu(d)}{d^2}+\frac{1}{2}x^2\sum_{d \le 2x, d \equiv 0 \pmod{2}} \frac{\mu(d)}{d^2}$. The first term is basically $\frac{1}{2}x^2\sum_{d=1}^\infty \frac{\mu(d)}{d^2} = \frac{3}{\pi^2} x^2$. The second term can be figured out also. So that's how the $\pi^2$'s come about.

One can do $f(n,a,k)$ similarly.