When is $\frac{a^2+b}{b^2+a}$ an integer?

For a fixed integer $k > 0$ we get solutions to $$ \frac{x^2 + y}{x + y^2} = k $$ by a Pell type equation. It turns into $$ (2ky -1)^2 - k (2x-k)^2 = 1 - k^3 $$

You asked about $k=2.$ There are two systems of degree two linear recurrences for $w^2 - 2 v^2 = -7.$

First we have $w_n = -1, 5, 31, 181, $ with $u_n = 2, 4, 22, 128.$ The orbit relation is $$ w_{n+1} = 3 w_n + 4 u_n, \; \; \; u_{n+1} = 2 w_n + 3 u_n. $$ When $w \equiv 3 \pmod 4,$ we get $y > 0$ with $2ky - 1 = w.$

We also have $w_n = 1, 11, 65, 379, $ with $u_n = 2, 8, 46, 268.$ The orbit relation is $$ w_{n+1} = 3 w_n + 4 u_n, \; \; \; u_{n+1} = 2 w_n + 3 u_n. $$ When $w \equiv 3 \pmod 4,$ we get $y > 0$ with $2ky - 1 = w.$

My take on the outcome is that it is not natural to require $y > 0.$ Makes it messy.

For example, with $w = 12875,$ we get $4y = 12876,$ $y = 3219.$ Also $v = 9104, $ $2x - 2 = 9104,$ $2x = 9106, $ $x = 4553.$ Alright, $x=4553, y = 3219.$ $$ \frac{x^2 + y}{y^2 + x} = \frac{4553^2 + 3219}{ 3219^2 + 4553} = \frac{20733028}{10366514} = 2 $$

jagy@phobeusjunior:~$ ./Pell_Target_Fundamental
  Automorphism matrix:  
    3   4
    2   3
  Automorphism backwards:  
    3   -4
    -2   3

  3^2 - 2 2^2 = 1

 w^2 - 2 v^2 = -7

Wed Nov 29 17:05:09 PST 2017

w:  1  v:  2  SEED   KEEP +- 
w:  5  v:  4  SEED   BACK ONE STEP  -1 ,  2
w:  11  v:  8
w:  31  v:  22
w:  65  v:  46
w:  181  v:  128
w:  379  v:  268
w:  1055  v:  746
w:  2209  v:  1562
w:  6149  v:  4348
w:  12875  v:  9104
w:  35839  v:  25342
w:  75041  v:  53062
w:  208885  v:  147704
w:  437371  v:  309268
w:  1217471  v:  860882
w:  2549185  v:  1802546
w:  7095941  v:  5017588
w:  14857739  v:  10506008
w:  41358175  v:  29244646

Wed Nov 29 17:06:09 PST 2017

 w^2 - 2 v^2 = -7

I have found three infinite classes of solutions to $a^2+b = n(b^2+a)$. These were derived assuming that $a$ and $b$ are relatively prime. All three have been verified by Wolfy.

Note added later: Two other solutions are $a=5, b=2, n=3$ and $a=5, b=3, n=2$.

The first two are one-parameter solutions, the parameter being $m$.

The first is

$n=m^2+m+1,\\ a =m^3+m^2+2m+1,\\ b = m^2+1 $.

The second is

$n =2m^2-m+1,\\ b = 2m+1,\\ a =4m^2+1 $.

The third is a two-parameter solution, the parameters being $u$ and $k$.

$n =u^2k^2-2uk+k+1,\\ b = u^3k^2-3u^2k+3u+uk-1,\\ a =k^3 u^4 - 4 k^2 u^3 + 2 k^2 u^2 + 6 k u^2 - 4 k u + k - 3 u+2 $.

Amusingly, if we put $u=0$ this gives $n=k+1, b=-1, a=k+2$ which gives $\dfrac{(k+2)^2-1}{3+k} =\dfrac{k^2+4k+3}{3+k} =\dfrac{(k+3)(k+1)}{3+k} =k+1 $. This is the same as $a=k+1, b=-1, n=k$.

My work that follows allows others to be derived.

A more general class is

$n=m^2+k,\\ a =m(2m+\dfrac{m^3+1}{k})+k,\\ b =m+\dfrac{m^3+1}{k} $

where $k | (m^3+1)$ (always true for $k = m+1,$ $k = m^2-m+1$, or $m = uk-1$).

Here is my derivation.

If $a^2+b = n(b^2+a)$, then $a^2-na = nb^2-b$ or $a(a-n) = b(nb-1)$.

If $(a, b) = 1$ then $nb-1 = ma$ and $a-n = mb$.

The following paragraph is a new addition.

Note: If $m(nb-1) = a$ and $m(a-n) = b$ then $m$ divides both $a$ and $b$ so $m=1$. Then $a = nb-1 =n(a-n)-1 $ so $a(n-1) =n^2+1 =(n-1)(n+1)+2 $ or $a = n+1+\frac{2}{n-1} $ so $n=2$ or $3$. If $n=2$ then $a=5, b=3$; if $n=3$ then $a=5, b=2$.

Therefore $a = mb+n$ so $nb-1 =m(mb+n) =m^2b+mn $ or $b(n-m^2) =mn+1 $ so that $(n-m^2)|(mn+1)$ and $m^2 < n$.

If $n = m^2+k$, then $mn+1 =m(m^2+k)+1 =m^3+km+1 $ so that $bk = m^3+km+1 $ or $b = m+\dfrac{m^3+1}{k} $.

If $k | (m^3+1)$ (always true for $k = m+1$ and $k = m^2-m+1$), then

$\begin{array}\\ a &= mb+n\\ &=m(m+\dfrac{m^3+1}{k})+m^2+k\\ &=m(2m+\dfrac{m^3+1}{k})+k\\ \end{array} $

If $k = m+1$, this gives $n =m^2+k =m^2+m+1 $, $b = m+m^2-m+1 = m^2+1 $ and $a =mb+n =m(m^2+1)+m^2+m+1 =m^3+m^2+2m+1 $.

If $k = m^2-m+1$, this gives $n =m^2+k =m^2+m^2-m+1 =2m^2-m+1 $, $b = m+m+1 = 2m+1 $ and $a =mb+n =m(2m+1)+2m^2-m+1 =4m^2+1 $.

If $m = uk-1$, then $m^3 = u^3k^3-3u^2k^2+3uk-1$ so $\dfrac{m^3+1}{k} =u^3k^2-3u^2k+3u $ and

$n =(uk-1)^2+k =u^2k^2-2uk+k+1,\\ b = u^3k^2-3u^2k+3u+uk-1,\\ a = (uk-1)(u^3k^2-3u^2k+3u+uk-1)+u^2k^2-2uk+k+1\\ \quad=k^3 u^4 - 4 k^2 u^3 + 2 k^2 u^2 + 6 k u^2 - 4 k u + k - 3 u+2 $

(The expansion of $a$ was done by Wolfy.)