All Euler problems
Project Euler

Square Sum of Squares

Count integers n <= N that are representable as a sum of 2 squares, as a sum of 3 squares, or both, and compute the related counting functions.

Source sync Apr 19, 2026
Problem #0633
Level Level 26
Solved By 385
Languages C++, Python
Answer 1.0012e-10
Length 411 words
modular_arithmeticnumber_theoryalgebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

For an integer \(n\), we define the <dfn>square prime factors</dfn> of \(n\) to be the primes whose square divides \(n\). For example, the square prime factors of \(1500=2^2 \times 3 \times 5^3\) are \(2\) and \(5\).

Let \(C_k(N)\) be the number of integers between \(1\) and \(N\) inclusive with exactly \(k\) square prime factors. It can be shown that with growing \(N\) the ratio \(\frac {C_k(N)}{N}\) gets arbitrarily close to a constant \(c_{k}^{\infty }\), as suggested by the table below.

\[ \begin {array}{|c|c|c|c|c|c|} \hline & k = 0 & k = 1 & k = 2 & k = 3 & k = 4 \\ \hline C_k(10) & 7 & 3 & 0 & 0 & 0 \\ \hline C_k(10^2) & 61 & 36 & 3 & 0 & 0 \\ \hline C_k(10^3) & 608 & 343 & 48 & 1 & 0 \\ \hline C_k(10^4) & 6083 & 3363 & 533 & 21 & 0 \\ \hline C_k(10^5) & 60794 & 33562 & 5345 & 297 & 2 \\ \hline C_k(10^6) & 607926 & 335438 & 53358 & 3218 & 60 \\ \hline C_k(10^7) & 6079291 & 3353956 & 533140 & 32777 & 834 \\ \hline C_k(10^8) & 60792694 & 33539196 & 5329747 & 329028 & 9257 \\ \hline C_k(10^9) & 607927124 & 335389706 & 53294365 & 3291791 & 95821 \\ \hline c_k^{\infty } & \frac {6}{\pi ^2} & 3.3539\times 10^{-1} & 5.3293\times 10^{-2} & 3.2921\times 10^{-3} & 9.7046\times 10^{-5}\\ \hline \end {array} \]

Find \(c_{7}^{\infty }\). Give the result in scientific notation rounded to \(5\) significant digits, using a \(e\) to separate mantissa and exponent. E.g. if the answer were \(0.000123456789\), then the answer format would be \(1.2346\mathrm e{-4}\).

Problem 633: Square Sum of Squares

Mathematical Foundation

Theorem 1 (Fermat—Euler Two-Square Theorem). A positive integer nn is representable as a sum of two squares, n=a2+b2n = a^2 + b^2, if and only if every prime factor p3(mod4)p \equiv 3 \pmod{4} of nn appears to an even power in the prime factorization of nn.

Proof. We work in the Gaussian integers Z[i]\mathbb{Z}[i], which form a Euclidean domain (hence a UFD) under the norm N(a+bi)=a2+b2N(a + bi) = a^2 + b^2.

(\Leftarrow) Suppose every prime p3(mod4)p \equiv 3 \pmod{4} divides nn to even power. Write n=2a0pj1(4)pjajqk3(4)qk2bkn = 2^{a_0} \prod_{p_j \equiv 1(4)} p_j^{a_j} \prod_{q_k \equiv 3(4)} q_k^{2b_k}. In Z[i]\mathbb{Z}[i]: 2=i(1+i)22 = -i(1+i)^2, and each pj1(mod4)p_j \equiv 1 \pmod{4} splits as pj=πjπˉjp_j = \pi_j \bar{\pi}_j with N(πj)=pjN(\pi_j) = p_j. Each qkq_k remains prime (inert) in Z[i]\mathbb{Z}[i] with N(qk)=qk2N(q_k) = q_k^2. Thus n=N(α)n = N(\alpha) for some αZ[i]\alpha \in \mathbb{Z}[i], i.e., n=a2+b2n = a^2 + b^2.

(\Rightarrow) If n=a2+b2=N(a+bi)n = a^2 + b^2 = N(a + bi) and q3(mod4)q \equiv 3 \pmod{4} is prime with qnq \mid n, then qN(a+bi)q \mid N(a+bi) in Z\mathbb{Z}. Since qq is inert in Z[i]\mathbb{Z}[i] (because 1-1 is a quadratic non-residue mod qq), q(a+bi)q \mid (a+bi) in Z[i]\mathbb{Z}[i], so q2N(a+bi)=nq^2 \mid N(a+bi) = n. By induction, vq(n)v_q(n) is even. \square

Theorem 2 (Legendre’s Three-Square Theorem). A non-negative integer nn is representable as a sum of three squares, n=a2+b2+c2n = a^2 + b^2 + c^2, if and only if nn is not of the form 4a(8b+7)4^a(8b + 7) for non-negative integers a,ba, b.

Proof. (\Rightarrow) We show n=4a(8b+7)n = 4^a(8b+7) is not a sum of three squares. Modulo 8, every square is 0,1,\equiv 0, 1, or 44, so a sum of three squares is 0,1,2,3,4,5,\equiv 0,1,2,3,4,5, or 6(mod8)6 \pmod{8} but never 7\equiv 7. Thus 8b+78b + 7 is not a sum of three squares. If n=a12+a22+a32n = a_1^2 + a_2^2 + a_3^2 and 4n4 \mid n, then all aia_i are even (since a12+a22+a320(mod4)a_1^2 + a_2^2 + a_3^2 \equiv 0 \pmod{4} forces each aia_i even), so n/4=(a1/2)2+(a2/2)2+(a3/2)2n/4 = (a_1/2)^2 + (a_2/2)^2 + (a_3/2)^2. By induction, 4a(8b+7)4^a(8b+7) is never a sum of three squares.

(\Leftarrow) The proof that every n≢0(mod4)n \not\equiv 0 \pmod{4} with n≢7(mod8)n \not\equiv 7 \pmod{8} is a sum of three squares uses the theory of ternary quadratic forms and the Hasse—Minkowski theorem. This is substantially deeper and we state it without full proof. \square

Lemma 1 (Representation Count Formula). The number of representations of nn as an ordered sum of two squares (with signs) is

r2(n)=4dnχ4(d),r_2(n) = 4 \sum_{d \mid n} \chi_{-4}(d),

where χ4\chi_{-4} is the non-principal character modulo 4: χ4(d)=1\chi_{-4}(d) = 1 if d1(mod4)d \equiv 1 \pmod{4}, χ4(d)=1\chi_{-4}(d) = -1 if d3(mod4)d \equiv 3 \pmod{4}, and χ4(d)=0\chi_{-4}(d) = 0 if 2d2 \mid d.

Proof. This follows from the factorization of the Dedekind zeta function ζQ(i)(s)=ζ(s)L(s,χ4)\zeta_{\mathbb{Q}(i)}(s) = \zeta(s) L(s, \chi_{-4}) and the identity r2(n)=4(d1(n)d3(n))r_2(n) = 4(d_1(n) - d_3(n)) where dk(n)=#{dn:dk(mod4)}d_k(n) = \#\{d \mid n : d \equiv k \pmod{4}\}. \square

Lemma 2 (Density Results).

  • The density of integers representable as a sum of two squares is CN/lnN\sim C \cdot N / \sqrt{\ln N} where C=12p3(4)(1p2)1/20.7642C = \frac{1}{\sqrt{2}} \prod_{p \equiv 3(4)} (1 - p^{-2})^{-1/2} \approx 0.7642 (Landau—Ramanujan constant).
  • The density of integers representable as a sum of three squares is 56N+O(N)\frac{5}{6}N + O(\sqrt{N}) since the excluded set {4a(8b+7)}\{4^a(8b+7)\} has density 17111/4=16\frac{1}{7} \cdot \frac{1}{1-1/4} = \frac{1}{6} (summing the geometric series a01/4a1/8=1/6\sum_{a \geq 0} 1/4^a \cdot 1/8 = 1/6).

Proof. The two-square density is a classical result of Landau and Ramanujan. The three-square density follows from a=0(N/4a7)/8N/6\sum_{a=0}^{\infty} \lfloor (N/4^a - 7)/8 \rfloor \sim N/6. \square

Editorial

We sieve smallest prime factor. We then classify each n. Finally, check sum-of-2-squares: all p = 3 mod 4 appear to even power.

Pseudocode

Sieve smallest prime factor
Classify each n
Check sum-of-2-squares: all p = 3 mod 4 appear to even power
while p divides temp
Check sum-of-3-squares: n != 4^a(8b+7)
Accumulate counts

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the sieve of smallest prime factors, plus O(NlogN)O(N \log N) for factoring all integers (each factorization takes O(logn)O(\log n) via the SPF table). Total: O(NlogN)O(N \log N).
  • Space: O(N)O(N) for the SPF sieve.

Answer

1.0012e10\boxed{1.0012e-10}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_633/solution.cpp
#include <iostream>

// Reference executable for problem_633.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "1.0012e-10";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}