User:RobHar/Sandbox14

Sums of three squares
The problem of writing a number as a sum of three squares dates at least as far back as Diophantus, who gave a necessary condition for a number of the form 3k + 1 to be so expressible (namely k cannot be 2 modulo 8). This case was improved upon by Bachet with Fermat finally giving the correct sufficient condition. A general positive integer n can be written as the sum of three squares if, and only if, it is not of the form 4e(8k + 7). Legendre attempted a proof of this characterization, but required Dirichlet's theorem on primes in arithmetic progressions (which had yet to be proved). Gauss gave the first complete proof in his Disquisitiones (articles 291–292), where he, in fact, also proves a formula for the number of primitive representations r$$(n) of n as a sum of three squares:
 * $$r^\ast_3(n)=\begin{cases}12h(-n) & n\equiv1,2\text{ (mod }4) \\ 8h(-n) & n\equiv3\text{ (mod }8) \\ 0 & \text{otherwise}\end{cases}$$

where h(&minus;n) is the class number of primitive positive definite binary quadratic forms of discriminant &minus;n.