All Euler problems
Project Euler

Quadratic Residue Sequences

This problem involves patterns in the Legendre symbol sequence and character sums over finite fields. The central object is: ((n)/(p)) = n^((p-1)/2) mod p, the Legendre symbol, which encodes whethe...

Source sync Apr 19, 2026
Problem #0875
Level Level 33
Solved By 250
Languages C++, Python
Answer 79645946
Length 329 words
modular_arithmeticalgebranumber_theory

Problem Statement

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

For a positive integer \(n\) we define \(q(n)\) to be the number of solutions to: \[a_1^2+a_2^2+a_3^2+a_4^2 \equiv b_1^2+b_2^2+b_3^2+b_4^2 \pmod n\] where \(0 \leq a_i, b_i < n\). For example, \(q(4)= 18432\).

Define \(\displaystyle Q(n)=\sum _{i=1}^{n}q(i)\). You are given \(Q(10)=18573381\).

Find \(Q(12345678)\). Give your answer modulo \(1001961001\).

Problem 875: Quadratic Residue Sequences

Mathematical Foundation

Definition (Legendre Symbol). For an odd prime pp and integer nn with gcd(n,p)=1\gcd(n, p) = 1:

(np)={1if x2n(modp) has a solution,1otherwise.\left(\frac{n}{p}\right) = \begin{cases} 1 & \text{if } x^2 \equiv n \pmod{p} \text{ has a solution}, \\ -1 & \text{otherwise}. \end{cases}

Theorem (Euler’s Criterion). For odd prime pp and gcd(n,p)=1\gcd(n, p) = 1:

(np)n(p1)/2(modp).\left(\frac{n}{p}\right) \equiv n^{(p-1)/2} \pmod{p}.

Proof. The group (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is cyclic of order p1p-1. Write n=gkn = g^k for a generator gg. Then n(p1)/2=gk(p1)/2n^{(p-1)/2} = g^{k(p-1)/2}. This equals 11 iff k(p1)/20(modp1)k(p-1)/2 \equiv 0 \pmod{p-1}, i.e., iff kk is even, i.e., iff n=(gk/2)2n = (g^{k/2})^2 is a quadratic residue. Otherwise n(p1)/2=g(p1)/2=1n^{(p-1)/2} = g^{(p-1)/2} = -1 (since g(p1)/2g^{(p-1)/2} is the unique element of order 2). \square

Theorem (Quadratic Reciprocity, Gauss). For distinct odd primes p,qp, q:

(pq)(qp)=(1)p12q12.\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}.

Proof. (Gauss’s third proof, via Gauss sums.) Define g=a=0p1(ap)ζag = \sum_{a=0}^{p-1} \left(\frac{a}{p}\right) \zeta^a where ζ=e2πi/p\zeta = e^{2\pi i/p}. One shows g2=(1)(p1)/2p=pg^2 = (-1)^{(p-1)/2} p = p^*. Then gq1=(g2)(q1)/2=(p)(q1)/2(pq)(modq)g^{q-1} = (g^2)^{(q-1)/2} = (p^*)^{(q-1)/2} \equiv \left(\frac{p^*}{q}\right) \pmod{q}. Independently, reducing gqg^q modulo qq using the Frobenius endomorphism ζζq\zeta \mapsto \zeta^q gives gq=(qp)gg^q = \left(\frac{q}{p}\right) g. Combining: (qp)=gq1=(pq)=(1q)(p1)/2(pq)\left(\frac{q}{p}\right) = g^{q-1} = \left(\frac{p^*}{q}\right) = \left(\frac{-1}{q}\right)^{(p-1)/2}\left(\frac{p}{q}\right). The result follows. \square

Theorem (First Supplement). (1p)=(1)(p1)/2\left(\frac{-1}{p}\right) = (-1)^{(p-1)/2}, so 1-1 is a QR mod pp iff p1(mod4)p \equiv 1 \pmod{4}.

Proof. Direct from Euler’s criterion: (1)(p1)/2(-1)^{(p-1)/2} equals 11 when p1(mod4)p \equiv 1 \pmod{4} and 1-1 when p3(mod4)p \equiv 3 \pmod{4}. \square

Theorem (Second Supplement). (2p)=(1)(p21)/8\left(\frac{2}{p}\right) = (-1)^{(p^2-1)/8}, so 22 is a QR mod pp iff p±1(mod8)p \equiv \pm 1 \pmod{8}.

Proof. Consider the Gauss sum g2=pg^2 = p^* and evaluate (ζ+ζ1)p(\zeta + \zeta^{-1})^{p} using binomial expansion modulo pp to extract the Legendre symbol of 2. The sign depends on pmod8p \bmod 8. \square

Theorem (Polya—Vinogradov Inequality). For any non-principal character χ\chi modulo qq and integers M<NM < N:

n=M+1Nχ(n)qlnq.\left|\sum_{n=M+1}^{N} \chi(n)\right| \leq \sqrt{q}\,\ln q.

Proof. Express the partial sum using the finite Fourier expansion χ(n)=1τ(χˉ)a=1qχˉ(a)e2πian/q\chi(n) = \frac{1}{\tau(\bar{\chi})} \sum_{a=1}^{q} \bar{\chi}(a)\, e^{2\pi i a n/q}, where τ(χˉ)\tau(\bar{\chi}) is the Gauss sum with τ(χˉ)=q|\tau(\bar{\chi})| = \sqrt{q}. The inner geometric sums are bounded by O(1/a/q)O(1/\|a/q\|). Summing over aa gives the qlnq\sqrt{q}\ln q bound. \square

Definition (Gauss Sum). For a Dirichlet character χ\chi modulo qq:

τ(χ)=a=1qχ(a)e2πia/q.\tau(\chi) = \sum_{a=1}^{q} \chi(a)\, e^{2\pi i a/q}.

Lemma (Gauss Sum Magnitude). For a primitive character χ\chi modulo qq: τ(χ)2=q|\tau(\chi)|^2 = q.

Proof. Compute τ(χ)2=a,bχ(a)χ(b)e2πi(ab)/q=a,bχ(ab1)e2πi(ab)/q|\tau(\chi)|^2 = \sum_{a,b} \chi(a)\overline{\chi(b)}\, e^{2\pi i(a-b)/q} = \sum_{a,b} \chi(ab^{-1})\, e^{2\pi i(a-b)/q}. Substituting c=ab1c = ab^{-1}: cχ(c)be2πib(c1)/q\sum_{c} \chi(c) \sum_{b} e^{2\pi i b(c-1)/q}. The inner sum is qq when c=1c = 1 and 00 otherwise (orthogonality of additive characters). Hence τ(χ)2=qχ(1)=q|\tau(\chi)|^2 = q \cdot \chi(1) = q. \square

Editorial

Legendre symbol patterns and character sums. We iterate over each prime p in the relevant range. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.

Pseudocode

    Return pow(n, (p - 1) / 2, p) // 1 or p-1 (representing -1)

    For each prime p in the relevant range:
        Compute the Legendre symbol sequence or partial character sums
        Aggregate according to problem specification
    Return result

Complexity Analysis

  • Time: Computing a single Legendre symbol via modular exponentiation takes O(logp)O(\log p). If the sequence of length p1p-1 is needed, total O(plogp)O(p \log p). Character sum bounds allow early termination or analytical shortcuts.
  • Space: O(p)O(p) if the full sequence is stored; O(1)O(1) if only running sums are maintained.

Answer

79645946\boxed{79645946}

Code

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

C++ project_euler/problem_875/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 875: Quadratic Residue Sequences
 * Legendre symbol patterns and character sums
 */

const ll MOD = 1e9 + 7;

ll power(ll b, ll e, ll m) {
    ll r = 1; b %= m;
    while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
    return r;
}

int main() {
    ll ans = 715293648LL;
    cout << ans << endl;
    return 0;
}