Finding integer solutions to $n^2 = 2d^2 - 2d + 1$ I'm trying to find the smallest integer solution to 

$$n^2 = 2d^2 - 2d + 1$$

Additional constraints: $$d &gt; 10^{12}, n &gt; 0$$

I wrote a computer program to bruteforce it, but that is too slow.

This page says that I can consider the equation modulo some prime to find whether it has solutions, but I already know that it has a solution, I just don't know what it is.
It's $$n^2=d^2+(d-1)^2$$ and we got Pythagorean triples because we can assume that $d-1&gt;0$, 

otherwise it's or obvious or the same. 

Since $gcd(d,d-1)=1$, we have two cases.


$d=2mn$ and $d-1=m^2-n^2$, where $gcd(m,n)=1$, $m&gt;n$ and $mn$ is even;
$d=m^2-n^2$ and $d-1=2mn$.


We got two Pell's equations:
$$(m+n)^2=1+2m^2$$ and $$(m-n)^2=1+2n^2.$$
They are $y^2=1+2x^2$ with a minimal solution $(2,3)$ and from here we can get all solutions.
Found a solution.

Valid values of d have a ratio of approx 5.82842712474619 between them. (Is this ratio significant in some way? Damned if I know.)

In other words, if you know that 1235216565974041 is a valid value, then you can guess that 7199369738058942 is a valid value, which is only 2 away from 7199369738058940, which is correct.

This makes the problem bruteforceable, and solves my problem.