Solution (source code)

= Solution

Suppose $g=[A,B,C]$ is obtained from $f=[a,b,c]$ by such a change of variables. Expanding shows that each of $A,B,C$ is an integer linear combination of $a,b,c$. Therefore every common divisor of $a,b,c$ divides $A,B,C$.

The inverse of a matrix in $\operatorname{SL}_2(\mathbb Z)$ again has integral entries and determinant one. Applying the same argument to $U^{-1}$ shows that every common divisor of $A,B,C$ divides $a,b,c$. The two coefficient triples consequently have the same greatest common divisor, up to sign, so one form is primitive exactly when the other is.