All Euler problems
Project Euler

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).

Source sync Apr 19, 2026
Problem #0938
Level Level 14
Solved By 1,143
Languages C++, Python
Answer 0.2928967987
Length 309 words
modular_arithmeticgeometryoptimization

Problem Statement

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

A deck of cards contains red cards and black cards.
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 be the probability that this colour is black.

You are given , and .

Find . Give your answer with 10 digits after the decimal point.

Problem 938: Circle Lattice Points

Mathematical Foundation

Definition. r2(n)=#{(x,y)Z2:x2+y2=n}r_2(n) = \#\{(x, y) \in \mathbb{Z}^2 : x^2 + y^2 = n\} is the number of representations of nn as a sum of two squares (counting order and signs).

Theorem 1 (Gauss circle asymptotics). C(r)=πr2+E(r)C(r) = \pi r^2 + E(r) where E(r)=O(r2/3)|E(r)| = O(r^{2/3}) (Huxley, 2003, improved the exponent to 131/208131/208).

Proof. The lattice points inside a circle of radius rr approximate the area πr2\pi r^2. The error term arises from the discrepancy between the discrete count and the continuous area. Gauss originally proved E(r)=O(r)|E(r)| = O(r); the sharper bound requires exponential sum techniques beyond the scope of this solution. \square

Theorem 2 (Representation count via characters). For n1n \geq 1:

r2(n)=4dnχ(d),r_2(n) = 4 \sum_{d \mid n} \chi(d),

where χ\chi is the non-principal Dirichlet character modulo 4: χ(d)=0\chi(d) = 0 if dd is even, χ(d)=(1)(d1)/2\chi(d) = (-1)^{(d-1)/2} if dd is odd.

Proof. This is a classical result. In the Gaussian integers Z[i]\mathbb{Z}[i], the norm N(a+bi)=a2+b2N(a + bi) = a^2 + b^2 is multiplicative, and r2(n)=4dnχ(d)r_2(n) = 4 \sum_{d \mid n} \chi(d) follows from the factorization theory in Z[i]\mathbb{Z}[i] (specifically, the number of Gaussian integers of norm nn is dnχ(d)\sum_{d \mid n} \chi(d), and the factor of 4 accounts for the four units {±1,±i}\{\pm 1, \pm i\}). \square

Theorem 3 (Quadrant formula for C(r)C(r)). For integer r0r \geq 0:

C(r)=1+4x=1rr2x2+4r.C(r) = 1 + 4\sum_{x=1}^{r} \left\lfloor \sqrt{r^2 - x^2} \right\rfloor + 4r.

Proof. Partition the lattice points by quadrant. The origin contributes 1. The positive xx-axis (y=0y = 0, x>0x > 0) contributes rr points, and by symmetry the four semi-axes contribute 4r4r. For x1x \geq 1, the number of lattice points with x2+y2r2x^2 + y^2 \leq r^2 and y1y \geq 1 is r2x2\lfloor \sqrt{r^2 - x^2} \rfloor. Multiplying by 4 for the four quadrants: C(r)=1+4r+4x=1rr2x2C(r) = 1 + 4r + 4\sum_{x=1}^{r} \lfloor \sqrt{r^2 - x^2} \rfloor. \square

Lemma 1 (Summation interchange for D(N)D(N)). We can write:

D(N)=r=1NC(r)=r=1Nn=0r2r2(n)=n=0N2r2(n)(Nn+1)+D(N) = \sum_{r=1}^{N} C(r) = \sum_{r=1}^{N} \sum_{n=0}^{r^2} r_2(n) = \sum_{n=0}^{N^2} r_2(n) \cdot (N - \lceil \sqrt{n} \rceil + 1)^+

where (x)+=max(x,0)(x)^+ = \max(x, 0) and n\lceil \sqrt{n} \rceil is the smallest rr with r2nr^2 \geq n.

Proof. The integer nn is counted in C(r)C(r) for every rr with r2nr^2 \geq n, i.e., rnr \geq \lceil \sqrt{n} \rceil. Among r=1,,Nr = 1, \ldots, N, this gives max(Nn+1,0)\max(N - \lceil \sqrt{n} \rceil + 1, 0) terms. \square

Editorial

Optimized approach using r2(n)r_2(n) 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): O ⁣(r=1Nr)=O(N2)O\!\left(\sum_{r=1}^{N} r\right) = O(N^2).
  • Time (sieve-based): O(N2logN)O(N^2 \log N) for the sieve of r2(n)r_2(n), plus O(N2)O(N^2) for the summation.
  • Space (direct): O(1)O(1).
  • Space (sieve-based): O(N2)O(N^2).

For N=105N = 10^5: direct approach requires 5×109\sim 5 \times 10^9 operations (tight but feasible with optimized inner loop); the sieve approach requires O(1010logN)O(10^{10} \log N) for the sieve, which is slower unless further optimized.

Answer

0.2928967987\boxed{0.2928967987}

Code

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

C++ project_euler/problem_938/solution.cpp
#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;
}