Project Euler Problem 233
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Project Euler 233 looks geometric on the surface, but it is really a number-theory problem. The challenge asks for values of N where the circle passing through (0, 0), (N, 0), (0, N), and (N, N) contains exactly 420 lattice points, then asks for the sum of all such N below a large limit.
Turn the Geometry into an Arithmetic Equation
The circle through those four corner points has center (N / 2, N / 2) and radius N / sqrt(2). Its equation is:
Multiply by four to avoid fractions:
Now define:
- '
a = 2x - N' - '
b = 2y - N'
Then the lattice-point count becomes the number of integer solutions to:
So the geometry has been converted into a sum-of-two-squares counting problem.
Use the Sum-of-Two-Squares Formula
For an integer m, the number of integer pairs solving a^2 + b^2 = m is commonly written as r2(m). A classic theorem gives:
Here:
- '
d1(m)counts divisors congruent to1 mod 4' - '
d3(m)counts divisors congruent to3 mod 4'
For this problem, m = 2N^2, and that simplifies beautifully. If:
where each p_i is a prime congruent to 1 mod 4 and each q_j is a prime congruent to 3 mod 4, then:
The exponents of primes congruent to 3 mod 4 do not change the count as long as they appear in a square, which they do inside N^2.
Because the target is 420 lattice points:
So:
That factorization is the heart of the problem.
What Exponent Patterns Are Possible
Since 105 = 3 * 5 * 7, the allowed odd factors of the form 2e + 1 lead to exponent patterns such as:
- '
1, 2, 3because3 * 5 * 7 = 105' - '
1, 17because3 * 35 = 105' - '
2, 10because5 * 21 = 105' - '
3, 7because7 * 15 = 105' - '
52because105 = 105'
These exponents apply only to primes congruent to 1 mod 4. The rest of N can be multiplied by powers of 2 and primes congruent to 3 mod 4 without changing the 420-point condition, as long as the product stays within the limit.
That observation is why brute force over every N up to the full bound is the wrong strategy. The efficient approach is to generate candidate cores from 1 mod 4 primes, then multiply by allowed square-safe factors.
A Small Prototype in Python
The following code shows the arithmetic core for smaller limits:
A search helper for candidate prime cores:
A full accepted solver adds:
- A prime generator split by
1 mod 4and3 mod 4 - Depth-first search over exponent patterns
- Pruning once the partial product exceeds the problem limit
- A second stage that multiplies each valid core by allowed factors from
2and3 mod 4primes
Why This Works Better Than Brute Force
The limit in Euler 233 is far too large for checking every N individually and counting lattice points with direct geometry. The theorem compresses the search into prime exponents, which is dramatically smaller.
This is a common Euler pattern: the problem statement looks like geometry, but the winning move is to replace point counting with a multiplicative number-theory formula.
Common Pitfalls
- Brute-forcing every
Nand testing points on a circle directly. - Forgetting that the count is for
2N^2, not forNitself. - Applying the divisor formula without separating
1 mod 4and3 mod 4primes. - Missing the fact that several exponent patterns can produce the same target count of 420.
Summary
- Euler 233 reduces to counting representations of
2N^2as a sum of two squares. - The lattice-point count becomes
4 * product(2e_i + 1)over primes congruent to1 mod 4. - Setting that expression equal to 420 gives the valid exponent patterns.
- Efficient solutions enumerate prime-factor structures rather than scanning all
N. - The key idea is turning geometry into multiplicative number theory.
Related reading
- Projected Gauss-Seidel for LCP
- Proof that Fowler's money allocation algorithm is correct
- Properly formatted multiplication table
- Prove a random generated number is uniform distributed
- Proving the primality of strong probable primes
- Pseudorandom Number Generator - Exponential Distribution
- Puzzle Find largest rectangle maximal rectangle problem
- puzzle N persons sitting on round table. No of ways of handshakes without crossing any other handshakes

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.