Circle Lattice Points
Let C(r) be the number of lattice points (x, y) in Z^2 with x^2 + y^2 <= r^2. Define D(N) = sum_(r=1)^N C(r). Find D(10^5) mod (10^9 + 7).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A deck of cards contains
A card is chosen uniformly randomly from the deck and removed. A second card is then chosen uniformly randomly from the cards remaining and removed.
- If both cards are red, they are discarded.
- If both cards are black, they are both put back in the deck.
- If they are different colours, the red card is put back in the deck and the black card is discarded.
Play ends when all the remaining cards in the deck are the same colour and let
You are given
Find
Problem 938: Circle Lattice Points
Mathematical Foundation
Definition. is the number of representations of as a sum of two squares (counting order and signs).
Theorem 1 (Gauss circle asymptotics). where (Huxley, 2003, improved the exponent to ).
Proof. The lattice points inside a circle of radius approximate the area . The error term arises from the discrepancy between the discrete count and the continuous area. Gauss originally proved ; the sharper bound requires exponential sum techniques beyond the scope of this solution.
Theorem 2 (Representation count via characters). For :
where is the non-principal Dirichlet character modulo 4: if is even, if is odd.
Proof. This is a classical result. In the Gaussian integers , the norm is multiplicative, and follows from the factorization theory in (specifically, the number of Gaussian integers of norm is , and the factor of 4 accounts for the four units ).
Theorem 3 (Quadrant formula for ). For integer :
Proof. Partition the lattice points by quadrant. The origin contributes 1. The positive -axis (, ) contributes points, and by symmetry the four semi-axes contribute . For , the number of lattice points with and is . Multiplying by 4 for the four quadrants: .
Lemma 1 (Summation interchange for ). We can write:
where and is the smallest with .
Proof. The integer is counted in for every with , i.e., . Among , this gives terms.
Editorial
Optimized approach using sieve:. We approach 1: Direct computation via quadrant formula (Theorem 3). We then iterate over r from 1 to N. Finally, c(r) = 1 + 4r + 4 * sum_{x=1}^{r} floor(sqrt(r^2 - x^2)).
Pseudocode
Approach 1: Direct computation via quadrant formula (Theorem 3)
for r from 1 to N
C(r) = 1 + 4*r + 4 * sum_{x=1}^{r} floor(sqrt(r^2 - x^2))
for x from 1 to r
Approach 2: Sieve r_2(n) for n up to N^2, then use Lemma 1
(More memory-intensive but potentially faster with prefix sums)
Sieve r_2(n) for n = 0, ..., N^2
Using Theorem 2: r_2(n) = 4 * sum_{d|n} chi(d)
Compute D(N) via Lemma 1
Complexity Analysis
- Time (direct): .
- Time (sieve-based): for the sieve of , plus for the summation.
- Space (direct): .
- Space (sieve-based): .
For : direct approach requires operations (tight but feasible with optimized inner loop); the sieve approach requires for the sieve, which is slower unless further optimized.
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=10000;
const long long MOD=1e9+7;
long long D=0;
for(int r=1;r<=N;r++){
long long cnt=0;
long long r2=(long long)r*r;
for(int x=-r;x<=r;x++){
long long ymax=(long long)sqrt((double)(r2-(long long)x*x));
while(ymax*ymax+(long long)x*x>r2) ymax--;
cnt+=2*ymax+1;
}
D=(D+cnt)%MOD;
}
cout<<D<<endl;
return 0;
}
"""
Problem 938: Circle Lattice Points
Compute D(N) = sum_{r=1}^{N} C(r), where C(r) counts the number of
lattice points (x, y) with x^2 + y^2 <= r^2 (Gauss circle problem).
The exact count is:
C(r) = 1 + 4 * sum_{k=0}^{inf} (floor(r^2/(4k+1)) - floor(r^2/(4k+3)))
or equivalently by direct enumeration over x.
The error term E(r) = C(r) - pi*r^2 is known to satisfy |E(r)| = O(r^{2/3}).
Results:
- C(1) = 5, C(2) = 13, C(3) = 29, C(5) = 81
- D(500) computed below
Methods:
1. count_lattice_points -- direct enumeration of C(r)
2. cumulative_D -- compute D(N) = sum C(r)
3. error_term -- E(r) = C(r) - pi*r^2
4. normalized_error -- E(r) / r^(2/3) for scaling analysis
"""
import numpy as np
def count_lattice_points(r):
"""C(r) = #{(x,y) in Z^2 : x^2 + y^2 <= r^2}."""
r2 = r * r
count = 0
for x in range(-r, r + 1):
max_y = int((r2 - x * x) ** 0.5)
count += 2 * max_y + 1
return count
def cumulative_D(N):
"""Compute D(N) and return (D(N), list of C(r))."""
total = 0
c_vals = []
for r in range(1, N + 1):
c = count_lattice_points(r)
c_vals.append(c)
total += c
return total, c_vals
def error_term(r, c_r):
"""E(r) = C(r) - pi * r^2."""
return c_r - np.pi * r ** 2
def normalized_error(r, c_r):
"""E(r) / r^(2/3) -- should remain bounded."""
if r == 0:
return 0.0
return error_term(r, c_r) / (r ** (2.0 / 3.0))
# Verification
assert count_lattice_points(1) == 5 # (0,0),(+-1,0),(0,+-1)
assert count_lattice_points(2) == 13
assert count_lattice_points(3) == 29
assert count_lattice_points(5) == 81
# Computation
N = 500
answer, c_vals = cumulative_D(N)
print(answer)