Gauss showed that the number of primitive representations ofan integer as sums of 3 squares is a simple factor times the classnumber of binary forms of a fixed discriminant. We present a generalprinciple on a quadratic form of $n$ variables that connectsa primitive representation of an integer by the form to aclass of an orthogonal group in dimension $n-1$. The case of 3 squaresis an easiest example.