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).
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 :
Proof. Since by the multiplicativity of the Legendre symbol, we have:
Note that the term contributes , so .
Complete the square: . As ranges over , also ranges over all of (since for odd ). Thus:
Since is a perfect square, . So:
It remains to evaluate .
Lemma (Evaluation of ). For any :
Proof of Lemma. Count solutions to over , i.e., , i.e., . Set , , so . For each , is determined, giving pairs . Recovering , each pair yields exactly one (since is odd). So the equation has exactly solutions .
Now, … more precisely:
Here we used for all (this is for QR, for QNR, for , and ). (Lemma)
Applying the lemma with to : . (Theorem 1)
Theorem 2 (Special Case ). .
Proof. The sum has one term (): .
Theorem 3 (Final Answer). for .
Proof. By Theorems 1 and 2: and for every odd prime . There are odd primes up to . Hence the sum is .
For : , so the answer is .
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: for the prime sieve; for the final computation once is known.
- Space: for the sieve.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 930: Quadratic Residue Patterns
For a prime p, define R(p) = sum_{a=1}^{p-1} L(a,p) * L(a+1,p) where L is
the Legendre symbol. Compute sum of R(p) for all primes p <= N = 10^5.
Key results:
- R(2) = 0.
- R(p) = -1 for all odd primes p (classical result from character sums).
- Therefore sum = 0 + (-1) * (number_of_odd_primes_up_to_N).
Methods:
- solve: O(N log log N) sieve, then apply the formula directly.
- R_brute: O(p^2) brute-force computation of R(p) for verification.
- legendre: Euler criterion for the Legendre symbol.
Complexity: O(N log log N) for sieve; verification is O(p) per prime.
"""
from collections import Counter
# Core functions
def legendre(a, p):
"""Compute the Legendre symbol (a/p) via Euler's criterion."""
if a % p == 0:
return 0
return 1 if pow(a, (p - 1) // 2, p) == 1 else -1
def R_brute(p):
"""Brute-force computation of R(p) = sum L(a,p)*L(a+1,p)."""
return sum(legendre(a, p) * legendre((a + 1) % p, p) for a in range(1, p))
def solve(N=10**5):
"""Sum of R(p) for primes p <= N. Uses R(2)=0, R(odd p)=-1."""
sieve = [True] * (N + 1)
sieve[0] = sieve[1] = False
for i in range(2, int(N**0.5) + 1):
if sieve[i]:
for j in range(i * i, N + 1, i):
sieve[j] = False
primes = [p for p in range(2, N + 1) if sieve[p]]
# R(2) = 0, R(p) = -1 for all odd primes
total = 0 + (-1) * (len(primes) - 1)
return total
def qr_set(p):
"""Return the set of quadratic residues mod p."""
return set(x * x % p for x in range(1, p))
# Verification
assert R_brute(3) == -1
assert R_brute(5) == -1
assert R_brute(7) == -1
assert R_brute(11) == -1
assert R_brute(13) == -1
assert R_brute(2) == 0
# Cross-check solve against brute force for small N
for test_N in [10, 50, 100]:
sieve_t = [True] * (test_N + 1)
sieve_t[0] = sieve_t[1] = False
for i in range(2, int(test_N**0.5) + 1):
if sieve_t[i]:
for j in range(i * i, test_N + 1, i):
sieve_t[j] = False
ps = [p for p in range(2, test_N + 1) if sieve_t[p]]
brute_sum = sum(R_brute(p) for p in ps)
assert solve(test_N) == brute_sum, f"Mismatch at N={test_N}"
# Compute answer
answer = solve()
print(answer)