All Euler problems
Project Euler

Quadratic Residue Patterns

For a prime p, define R(p) = sum_(a=1)^(p-1) ((a)/(p))((a+1)/(p)), where ((*)/(p)) is the Legendre symbol. Find sum_(p <= 10^5, p prime) R(p).

Source sync Apr 19, 2026
Problem #0930
Level Level 38
Solved By 137
Languages C++, Python
Answer 1.345679959251e12
Length 226 words
number_theoryalgebramodular_arithmetic

Problem Statement

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

Given \(Gn\ge 2\) bowls arranged in a circle, \(m\ge 2\) balls are distributed amongst them.

Initially the balls are distributed randomly: for each ball, a bowl is chosen equiprobably and independently of the other balls. After this is done, we start the following process:

1.
Choose one of the \(m\) balls equiprobably at random.
2.
Choose a direction to move - either clockwise or anticlockwise - again equiprobably at random.
3.
Move the chosen ball to the neighbouring bowl in the chosen direction.
4.
Return to step 1.

This process stops when all the \(m\) balls are located in the same bowl. Note that this may be after zero steps, if the balls happen to have been initially distributed all in the same bowl.

Let \(F(n, m)\) be the expected number of times we move a ball before the process stops. For example, \(F(2, 2) = \frac {1}{2}\), \(F(3, 2) = \frac {4}{3}\), \(F(2, 3) = \frac {9}{4}\), and \(F(4, 5) = \frac {6875}{24}\).

Let \(G(N, M) = \sum _{n=2}^N \sum _{m=2}^M F(n, m)\). For example, \(G(3, 3) = \frac {137}{12}\) and \(G(4, 5) = \frac {6277}{12}\). You are also given that \(G(6, 6) \approx 1.681521567954e4\) in scientific format with 12 significant digits after the decimal point.

Find \(G(12, 12)\). Give your answer in scientific format with 12 significant digits after the decimal point.

Problem 930: Quadratic Residue Patterns

Mathematical Foundation

Theorem 1 (Character Sum Identity). For every odd prime pp:

R(p)=a=1p1(ap)(a+1p)=1.R(p) = \sum_{a=1}^{p-1} \left(\frac{a}{p}\right)\left(\frac{a+1}{p}\right) = -1.

Proof. Since (ap)(a+1p)=(a(a+1)p)\left(\frac{a}{p}\right)\left(\frac{a+1}{p}\right) = \left(\frac{a(a+1)}{p}\right) by the multiplicativity of the Legendre symbol, we have:

R(p)=a=1p1(a2+ap).R(p) = \sum_{a=1}^{p-1}\left(\frac{a^2 + a}{p}\right).

Note that the a=0a = 0 term contributes (0p)=0\left(\frac{0}{p}\right) = 0, so R(p)=a=0p1(a2+ap)R(p) = \sum_{a=0}^{p-1}\left(\frac{a^2+a}{p}\right).

Complete the square: a2+a=(2a+1)2/41/4a^2 + a = (2a+1)^2/4 - 1/4. As aa ranges over Fp\mathbb{F}_p, u=2a+1u = 2a + 1 also ranges over all of Fp\mathbb{F}_p (since gcd(2,p)=1\gcd(2, p) = 1 for odd pp). Thus:

R(p)=uFp((u21)/4p)=(41p)uFp(u21p).R(p) = \sum_{u \in \mathbb{F}_p}\left(\frac{(u^2 - 1)/4}{p}\right) = \left(\frac{4^{-1}}{p}\right)\sum_{u \in \mathbb{F}_p}\left(\frac{u^2 - 1}{p}\right).

Since 4=224 = 2^2 is a perfect square, (41p)=((21)2p)=1\left(\frac{4^{-1}}{p}\right) = \left(\frac{(2^{-1})^2}{p}\right) = 1. So:

R(p)=u=0p1(u21p).(*)R(p) = \sum_{u=0}^{p-1}\left(\frac{u^2 - 1}{p}\right). \tag{*}

It remains to evaluate ()(*).

Lemma (Evaluation of u(u2cp)\sum_u \left(\frac{u^2 - c}{p}\right)). For any c≢0(modp)c \not\equiv 0 \pmod{p}:

u=0p1(u2cp)=1.\sum_{u=0}^{p-1}\left(\frac{u^2 - c}{p}\right) = -1.

Proof of Lemma. Count solutions to y2=u2cy^2 = u^2 - c over Fp\mathbb{F}_p, i.e., u2y2=cu^2 - y^2 = c, i.e., (uy)(u+y)=c(u-y)(u+y) = c. Set s=uys = u - y, t=u+yt = u + y, so st=cst = c. For each sFps \in \mathbb{F}_p^*, t=c/st = c/s is determined, giving p1p - 1 pairs (s,t)(s, t). Recovering (u,y)=((s+t)/2,(ts)/2)(u, y) = ((s+t)/2, (t-s)/2), each pair (s,t)(s,t) yields exactly one (u,y)(u, y) (since pp is odd). So the equation y2=u2cy^2 = u^2 - c has exactly p1p - 1 solutions (u,y)(u, y).

Now, u=0p1(u2cp)=u(number of y with y2=u2c)u1\sum_{u=0}^{p-1}\left(\frac{u^2 - c}{p}\right) = \sum_u \left(\text{number of } y \text{ with } y^2 = u^2-c\right) - \sum_u 1 … more precisely:

u(u2cp)=u(#{y:y2=u2c}1)=(p1)p=1.\sum_u\left(\frac{u^2-c}{p}\right) = \sum_u \left(\#\{y : y^2 = u^2-c\} - 1\right) = (p-1) - p = -1.

Here we used (vp)=#{y:y2=v}1\left(\frac{v}{p}\right) = \#\{y : y^2 = v\} - 1 for all vFpv \in \mathbb{F}_p (this is +1+1 for QR, 1-1 for QNR, 00 for v=0v = 0, and y1[y2=v]=1+(vp)\sum_{y} \mathbf{1}[y^2 = v] = 1 + \left(\frac{v}{p}\right)). \square (Lemma)

Applying the lemma with c=1c = 1 to ()(*): R(p)=1R(p) = -1. \square (Theorem 1)

Theorem 2 (Special Case p=2p = 2). R(2)=0R(2) = 0.

Proof. The sum has one term (a=1a = 1): R(2)=(12)(22)=10=0R(2) = \left(\frac{1}{2}\right)\left(\frac{2}{2}\right) = 1 \cdot 0 = 0. \square

Theorem 3 (Final Answer). pNp primeR(p)=(π(N)1)\displaystyle\sum_{\substack{p \leq N \\ p \text{ prime}}} R(p) = -(\pi(N) - 1) for N2N \geq 2.

Proof. By Theorems 1 and 2: R(2)=0R(2) = 0 and R(p)=1R(p) = -1 for every odd prime pNp \leq N. There are π(N)1\pi(N) - 1 odd primes up to NN. Hence the sum is 0+(1)(π(N)1)=(π(N)1)0 + (-1)(\pi(N) - 1) = -(\pi(N) - 1). \square

For N=105N = 10^5: π(105)=9592\pi(10^5) = 9592, so the answer is (95921)=9591-(9592 - 1) = -9591.

Editorial

Or equivalently. We count primes up to N. 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

Count primes up to N
Or equivalently
for p in primes
else

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the prime sieve; O(1)O(1) for the final computation once π(N)\pi(N) is known.
  • Space: O(N)O(N) for the sieve.

Answer

1.345679959251e12\boxed{1.345679959251e12}

Code

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

C++ project_euler/problem_930/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 100000;
    vector<bool> sieve(N + 1, true);
    sieve[0] = sieve[1] = false;
    for (int i = 2; i * i <= N; i++)
        if (sieve[i])
            for (int j = i*i; j <= N; j += i)
                sieve[j] = false;
    int prime_count = 0;
    for (int p = 3; p <= N; p++)
        if (sieve[p]) prime_count++;
    // R(2)=0, R(p)=-1 for odd primes
    cout << -prime_count << endl;
    return 0;
}