# Diophantine equation: solving $a^2+4n=b^2$

I found myself working with diophantine equations but I have no experience at all with them. Given an integer $$n$$, can I find two integers, $$a$$ and $$b$$, such that $$a^2+4n=b^2$$ How would you guys approach the problem?

Sure. With $$a=n-1, b=n+1$$ we have $$a^2+4n=n^2-2n+1+4n=n^2+2n+1=b^2$$

• Hi thank you too for your answer, are you aware of methods to find non-trivial solutions? – Lyn Cassidy Nov 26 '18 at 13:02
• all the answers are trivial. Rewrite your equation as $4n=(b-a)(b+a)$. Write $4n=(2m)\times (2k)$ for any factoring, $n=mk$ If $m≥k$ then solve $b+a=2m,b-a=2k$. – lulu Nov 26 '18 at 13:07
• So basically to get all the results you need to factorize n. I'll mark this as the correct solution. – Lyn Cassidy Nov 26 '18 at 13:27

$$4n=b^2-a^2$$

$$n=\dfrac{b+a}2\cdot\dfrac{b-a}2$$

If $$n=p\cdot q,\dfrac{b+a}2=p, \dfrac{b-a}2=q$$

Trivially $$q=1,p=?$$

• Hi, thanks for your answer. Are you aware of methods to find non-trivial solutions? – Lyn Cassidy Nov 26 '18 at 13:00

Here is a method to compute $$0\leq a,b\leq 1000$$ given n.

public static long[][] diophantine(long n)
{
long fourn= 4*n;
long[][] results = new long[2][1000];
int index=0;
for(int a=1;a<1000;a++)
{
double b=Math.sqrt(Math.pow((double)a, 2.0)+(double)fourn);
if((b % 1) == 0)
{
results[0][index]=a;
results[1][index]=(long)b;
index++;
}
}
return results;

}


SO for example, when $$n=50$$ gives(first column is the values of a and the second is the values of b :

• Hi, thanks for the effort, but this is what i was trying to avoid: no loops, just a straight up equation. – Lyn Cassidy Nov 26 '18 at 13:24

4 n = b^2 - a^2
n = g * h
4 n = (b - a) (b + a)
4 n = 2 g * 2 h
b - a = 2 g; b + a = 2 h

n = g h
a = h - g
b = h + g

if n = 7 then
(g=-7; h=-1 => a=6; b=-8 or
g=-1; h=-7 => a=-6; b=-8 or
g=1; h=7 => a=6; b=8 or
g=7; h=1 =>a=-6; b=8)