Discriminant criterion for prime representation by a binary quadratic form (source code)

= Discriminant criterion for prime representation by a binary quadratic form
{title2=$p\text{ represented}\iff d\text{ is a square modulo }p$}
{title2=$p\text{ represented by some form of discriminant }D\iff D\text{ is a square mod }p$}

Let $p$ be an <odd prime> and let $D$ be an <integer> congruent to zero or one <modulo> four. Some integral <binary quadratic form> of <discriminant of a binary quadratic form> $D$ represents $p$ if and only if $D$ is a square <modulo> $p$.

For necessity, a representing vector $(x,y)$ must have $\gcd(x,y)=1$, since a common <divisor> greater than one would make its square divide $p$. <Bézout's identity> completes this vector to the first column of a determinant-one integer <matrix>. Under that <proper equivalence>, the leading <polynomial coefficient> becomes $p$, so the transformed form is $[p,b,c]$. Its discriminant gives $D=b^2-4pc\equiv b^2\pmod p$.

Conversely, choose a root $b$ <modulo> $p$ and adjust its <parity> so $b^2\equiv D\pmod4$. Adding $p$ changes parity because $p$ is odd. Then
$$
\left[p,b,\frac{b^2-D}{4p}\right]
$$
has integer <polynomial coefficients>, discriminant $D$ and value $p$ at $(1,0)$. If $D<0$, it is positive definite. If $p\nmid D$, it is also a <primitive binary quadratic form>: a common <divisor> of its coefficients would have to be $p$, contradicting $p\nmid D$.

The conclusion concerns some form, not every form of that discriminant. For $D=-20,p=3$, the construction gives $[3,2,2]$, which represents three. The same-discriminant form $[1,0,5]$ does not represent three. Deciding the particular form requires the <proper equivalence> classes, not only the local square test. When $p\mid D$, imprimitive forms can occur, so primitiveness must be checked separately.