# Cornacchia's algorithm

In computational number theory, Cornacchia's algorithm is an algorithm for solving the Diophantine equation $x^{2}+dy^{2}=m$ , where $1\leq d and d and m are coprime. The algorithm was described in 1908 by Giuseppe Cornacchia.
First, find any solution to $r_{0}^{2}\equiv -d{\pmod {m}}$ (perhaps by using an algorithm listed here); if no such $r_{0}$ exist, there can be no primitive solution to the original equation. Without loss of generality, we can assume that r0m/2 (if not, then replace r0 with m - r0, which will still be a root of -d). Then use the Euclidean algorithm to find $r_{1}\equiv m{\pmod {r_{0}}}$ , $r_{2}\equiv r_{0}{\pmod {r_{1}}}$ and so on; stop when $r_{k}<{\sqrt {m}}$ . If $s={\sqrt {\tfrac {m-r_{k}^{2}}{d}}}$ is an integer, then the solution is $x=r_{k},y=s$ ; otherwise try another root of -d until either a solution is found or all roots have been exhausted. In this case there is no primitive solution.
Solve the equation $x^{2}+6y^{2}=103$ . A square root of −6 (mod 103) is 32, and 103 ≡ 7 (mod 32); since $7^{2}<103$ and ${\sqrt {\tfrac {103-7^{2}}{6}}}=3$ , there is a solution x = 7, y = 3.