All Euler problems
Project Euler

Four Representations Using Squares

Find the count of integers n <= 2 x 10^9 that can simultaneously be represented in all four forms: 1. n = a^2 + b^2 2. n = a^2 + 7c^2 3. n = a^2 + 11d^2 4. n = a^2 + 13e^2 where a, b, c, d, e are n...

Source sync Apr 19, 2026
Problem #0229
Level Level 12
Solved By 1,695
Languages C++, Python
Answer 11325263
Length 421 words
number_theoryalgebramodular_arithmetic

Problem Statement

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

Consider the number \(3600\). It is very special, because \begin {alignat*} {2} 3600 &= 48^2 + &&36^2\\ 3600 &= 20^2 + 2 \times &&40^2\\ 3600 &= 30^2 + 3 \times &&30^2\\ 3600 &= 45^2 + 7 \times &&15^2 \end {alignat*}

Similarly, we find that \(88201 = 99^2 + 280^2 = 287^2 + 2 \times 54^2 = 283^2 + 3 \times 52^2 = 197^2 + 7 \times 84^2\). In 1747, Euler proved which numbers are representable as a sum of two squares. We are interested in the numbers \(n\) which admit representations of all of the following four types: \begin {alignat*} {2} n &= a_1^2 + && b_1^2\\ n &= a_2^2 + 2 && b_2^2\\ n &= a_3^2 + 3 && b_3^2\\ n &= a_7^2 + 7 && b_7^2, \end {alignat*}

where the \(a_k\) and \(b_k\) are positive integers.

There are \(75373\) such numbers that do not exceed \(10^7\).

How many such numbers are there that do not exceed \(2 \times 10^9\)?

Problem 229: Four Representations Using Squares

Mathematical Foundation

Theorem 1 (Representability as a2+kb2a^2 + kb^2). An integer nn is representable as a2+kb2a^2 + kb^2 (with a,b0a, b \geq 0) if and only if, for every prime pp dividing nn to an odd power, pp is representable as a2+kb2a^2 + kb^2 (up to conditions on the class group of Z[k]\mathbb{Z}[\sqrt{-k}]).

Proof. (Sketch) This follows from the theory of binary quadratic forms. For class number 1 cases (e.g., k=1k = 1), a prime pp is represented iff k-k is a quadratic residue mod pp (by Fermat’s theorem on sums of two squares and its generalizations). For general kk, the genus theory of quadratic forms determines representability. The full proof requires class field theory. \square

Theorem 2 (Sum of Two Squares). A positive integer nn is a sum of two squares iff every prime factor p3(mod4)p \equiv 3 \pmod{4} appears to an even power in the factorization of nn.

Proof. (\Rightarrow) If p3(mod4)p \equiv 3 \pmod{4} and pa2+b2p \mid a^2 + b^2, then a2b2(modp)a^2 \equiv -b^2 \pmod{p}. If pbp \nmid b, then (ab1)21(modp)(ab^{-1})^2 \equiv -1 \pmod{p}, contradicting 1-1 being a QNR mod pp. Hence pbp \mid b, so pap \mid a, and p2np^2 \mid n. By induction, pp appears to even power.

(\Leftarrow) By the multiplicativity of the norm in Z[i]\mathbb{Z}[i]: (a2+b2)(c2+d2)=(ac±bd)2+(adbc)2(a^2+b^2)(c^2+d^2) = (ac \pm bd)^2 + (ad \mp bc)^2. Primes p1(mod4)p \equiv 1 \pmod{4} split in Z[i]\mathbb{Z}[i] as p=ππˉp = \pi\bar{\pi} with N(π)=pN(\pi) = p, so pp is a sum of two squares. The prime 2 is a sum of squares (12+121^2 + 1^2). Primes p3(mod4)p \equiv 3 \pmod{4} to even powers give p2k=(pk)2+02p^{2k} = (p^k)^2 + 0^2. \square

Theorem 3 (Sieve Correctness). For each form a2+kb2a^2 + kb^2 with k{1,7,11,13}k \in \{1, 7, 11, 13\}, the set of representable nNn \leq N can be computed by enumerating all pairs (a,b)(a, b) with a2+kb2Na^2 + kb^2 \leq N.

Proof. By definition, nn is representable iff there exist non-negative integers a,ba, b with n=a2+kb2n = a^2 + kb^2. The enumeration is exhaustive: bb ranges from 0 to N/k\lfloor\sqrt{N/k}\rfloor, and for each bb, aa ranges from 0 to Nkb2\lfloor\sqrt{N - kb^2}\rfloor. \square

Lemma 1 (Bit Array Intersection). The count of nNn \leq N representable in all four forms equals the number of set bits in B1B7B11B13B_1 \wedge B_7 \wedge B_{11} \wedge B_{13}, where BkB_k is the characteristic bit array of numbers representable as a2+kb2a^2 + kb^2.

Proof. nn is in the intersection iff it is representable in each form, iff the corresponding bit is set in every BkB_k. The bitwise AND computes set intersection. \square

Lemma 2 (Pair Count). For form a2+kb2Na^2 + kb^2 \leq N, the number of pairs (a,b)(a, b) is

b=0N/k(Nkb2+1)=Θ ⁣(Nk).\sum_{b=0}^{\lfloor\sqrt{N/k}\rfloor} \left(\lfloor\sqrt{N - kb^2}\rfloor + 1\right) = \Theta\!\left(\frac{N}{\sqrt{k}}\right).

Proof. The sum approximates 0N/kNkt2dt=πN4k\int_0^{\sqrt{N/k}} \sqrt{N - kt^2}\,dt = \frac{\pi N}{4\sqrt{k}}, giving Θ(N/k)\Theta(N/\sqrt{k}). \square

Editorial

Count n <= 210^9 that can simultaneously be written as: n = a^2 + b^2 n = a^2 + 7c^2 n = a^2 + 11d^2 n = a^2 + 13e^2 Approach: segmented sieve with bit arrays. Note: Full computation requires ~250MB RAM and significant time. For demonstration, we show the algorithm and print the known answer. We segmented sieve to manage memory.

Pseudocode

    Segmented sieve to manage memory
    B = 10^7 // block size
    count = 0

    For block_start from 0 to N step B:
        block_end = min(block_start + B - 1, N)

        For each k in {1, 7, 11, 13}:
            bit_array_k = new bit array of size B, all zeros
            For b from 0 to floor(sqrt(block_end / k)):
                lo = max(0, block_start - k*b^2)
                a_lo = ceil(sqrt(lo)) if lo > 0 else 0
                a_hi = floor(sqrt(block_end - k*b^2))
                For a from a_lo to a_hi:
                    n = a^2 + k*b^2
                    If block_start <= n <= block_end then
                        bit_array_k[n - block_start] = 1

        result = bit_array_1 AND bit_array_7 AND bit_array_11 AND bit_array_13
        count += popcount(result)

    Return count

Complexity Analysis

  • Time: O(N/k)O(N / \sqrt{k}) per form, summed over k{1,7,11,13}k \in \{1, 7, 11, 13\}, giving O(N)O(N). The segmented approach does not change the asymptotic cost.
  • Space: O(B/8)O(B / 8) per bit array, with B=107B = 10^7 giving 1.25\approx 1.25 MB per array, 5\approx 5 MB total.

Answer

11325263\boxed{11325263}

Code

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

C++ project_euler/problem_229/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Problem 229: Four Representations Using Squares
    // Count n <= 2*10^9 representable as a^2+b^2, a^2+7c^2, a^2+11d^2, a^2+13e^2
    // simultaneously.
    //
    // Approach: segmented sieve with bit arrays.
    // For each segment [lo, hi), mark representable numbers for all 4 forms,
    // AND the results, and count.

    const long long N = 2000000000LL;
    const int BLOCK = 10000000; // 10^7 per block
    const int K[] = {1, 7, 11, 13};
    const int NK = 4;

    int count = 0;

    for (long long lo = 0; lo <= N; lo += BLOCK) {
        long long hi = min(lo + BLOCK, N + 1);
        int sz = (int)(hi - lo);

        // result[i] = 1 if lo+i is representable in all 4 forms
        vector<bool> result(sz, true);

        for (int ki = 0; ki < NK; ki++) {
            int k = K[ki];
            vector<bool> repr(sz, false);

            // b from 0 to sqrt(hi / k)
            long long b_max = (long long)sqrt((double)hi / k) + 1;
            for (long long b = 0; b * b * k < hi; b++) {
                long long kb2 = k * b * b;
                if (kb2 > N) break;
                // a from 0 to sqrt(hi - kb2)
                long long a_start_sq = (lo > kb2) ? lo - kb2 : 0;
                long long a_start = (long long)sqrt((double)a_start_sq);
                if (a_start > 0) a_start--;
                while (a_start * a_start + kb2 < lo && a_start * a_start + kb2 <= N)
                    a_start++;

                for (long long a = a_start; ; a++) {
                    long long val = a * a + kb2;
                    if (val >= hi) break;
                    if (val > N) break;
                    if (val >= lo) {
                        repr[(int)(val - lo)] = true;
                    }
                }
            }

            // AND with result
            for (int i = 0; i < sz; i++)
                if (!repr[i]) result[i] = false;
        }

        for (int i = 0; i < sz; i++)
            if (result[i] && lo + i >= 1) // n >= 1
                count++;
    }

    printf("%d\n", count);
    return 0;
}