All Euler problems
Project Euler

Integer Points on Ellipses

Find the number of integer solutions (x, y) to 3x^2 + 5y^2 <= 10^6.

Source sync Apr 19, 2026
Problem #0952
Level Level 30
Solved By 302
Languages C++, Python
Answer 794394453
Length 301 words
geometryanalytic_mathbrute_force

Problem Statement

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

Given a prime \(p\) and a positive integer \(n < p\), let \(R(p, n)\) be the multiplicative order of \(p\) modulo \(n!\).

In other words, \(R(p, n)\) is the minimal positive integer \(r\) such that \[p^r \equiv 1 \pmod {n!}\] For example, \(R(7, 4) = 2\) and \(R(10^9 + 7, 12) = 17280\).

For example, \(R(7, 4) = 2\) and \(R(10^9 + 7, 12) = 17280\).

For example, \(R(7, 4) = 2\) and \(R(10^9 + 7, 12) = 17280\).

Problem 952: Integer Points on Ellipses

Mathematical Foundation

Theorem 1 (Generalized Gauss Circle Problem). The number of lattice points (x,y)Z2(x, y) \in \mathbb{Z}^2 inside the ellipse Ax2+By2NAx^2 + By^2 \le N satisfies

L(N)=πNAB+O(N1/2+ε)L(N) = \frac{\pi N}{\sqrt{AB}} + O(N^{1/2 + \varepsilon})

for any ε>0\varepsilon > 0.

Proof. The leading term equals the area of the ellipse Ax2+By2NAx^2 + By^2 \le N, which has semi-axes a=N/Aa = \sqrt{N/A} and b=N/Bb = \sqrt{N/B}, giving area πab=πN/AB\pi ab = \pi N/\sqrt{AB}. Each lattice point (x,y)(x,y) can be associated with the unit square [x,x+1)×[y,y+1)[x, x+1) \times [y, y+1). The discrepancy between L(N)L(N) and the area arises from unit squares that straddle the ellipse boundary. The boundary has arc length O(N)O(\sqrt{N}), and each boundary-straddling square contributes O(1)O(1) error, so the total error is O(N)O(\sqrt{N}). Sharper bounds (e.g., O(N131/416)O(N^{131/416}) via Huxley’s method) require deeper analytic techniques. \square

Lemma 1 (Slice Counting). For fixed xx with 3x2N3x^2 \le N, the number of integers yy satisfying 3x2+5y2N3x^2 + 5y^2 \le N is

2N3x25+1.2\left\lfloor\sqrt{\frac{N - 3x^2}{5}}\right\rfloor + 1.

Proof. The constraint 5y2N3x25y^2 \le N - 3x^2 yields y(N3x2)/5|y| \le \sqrt{(N - 3x^2)/5}. The integer solutions are y{M,M+1,,M}y \in \{-M, -M+1, \ldots, M\} where M=(N3x2)/5M = \lfloor\sqrt{(N-3x^2)/5}\rfloor. This set has cardinality 2M+12M + 1. \square

Theorem 2 (Exact Count via Slicing). The total lattice point count is

L(N)=x=N/3N/3(2N3x25+1).L(N) = \sum_{x = -\lfloor\sqrt{N/3}\rfloor}^{\lfloor\sqrt{N/3}\rfloor} \left(2\left\lfloor\sqrt{\frac{N - 3x^2}{5}}\right\rfloor + 1\right).

Proof. For each integer xx with xN/3|x| \le \lfloor\sqrt{N/3}\rfloor, we have 3x2N3x^2 \le N, so the slice is non-empty. Lemma 1 gives the count for each slice. Summing over all valid xx counts every lattice point (x,y)(x,y) with 3x2+5y2N3x^2 + 5y^2 \le N exactly once, since the slices partition the lattice points by their xx-coordinate. \square

Lemma 2 (Symmetry Reduction). By the symmetry (x,y)(x,y)(x, y) \mapsto (-x, y):

L(N)=(2M0+1)+2x=1N/3(2N3x25+1)L(N) = (2M_0 + 1) + 2\sum_{x=1}^{\lfloor\sqrt{N/3}\rfloor} \left(2\left\lfloor\sqrt{\frac{N - 3x^2}{5}}\right\rfloor + 1\right)

where M0=N/5M_0 = \lfloor\sqrt{N/5}\rfloor is the yy-range at x=0x = 0.

Proof. The x=0x = 0 slice contributes 2M0+12M_0 + 1 points. For x0x \ne 0, the slices at xx and x-x have identical yy-ranges (since 3x2=3(x)23x^2 = 3(-x)^2), so each pair contributes twice the count of a single slice. \square

Editorial

Count integer (x, y) with 3x^2 + 5y^2 <= N, where N = 10^6. The lattice point count for ellipse Ax^2 + By^2 <= N is approximately pi*N/sqrt(AB), with error O(sqrt(N)). We compute exactly by slicing: for each valid x, count y-values satisfying y^2 <= (N - 3x^2) / 5. Complexity: O(sqrt(N)) time, O(1) space.

Pseudocode

    x_max = floor(sqrt(N / 3))
    count = 0
    For x from -x_max to x_max:
        R = N - 3 * x * x
        y_max = floor(sqrt(R / 5))
        count = count + 2 * y_max + 1
    Return count

Complexity Analysis

  • Time: O(N/3)=O(N)O(\sqrt{N/3}) = O(\sqrt{N}) iterations of the outer loop, each performing O(1)O(1) work (one integer square root computation).
  • Space: O(1)O(1) beyond the loop variable and accumulator.

For N=106N = 10^6: approximately 577577 iterations.

Answer

794394453\boxed{794394453}

Code

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

C++ project_euler/problem_952/solution.cpp
/*
 * Problem 952: Integer Points on Ellipses
 *
 * Count integer (x, y) with 3x^2 + 5y^2 <= N, N = 10^6.
 *
 * For each x in [-sqrt(N/3), sqrt(N/3)], count y in [-M, M] where
 * M = floor(sqrt((N - 3x^2) / 5)).
 *
 * Complexity: O(sqrt(N)) time, O(1) space.
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
    const long long N = 1000000;
    long long count = 0;

    int max_x = (int)sqrt((double)N / 3.0);
    // Adjust for floating-point imprecision
    while (3LL * (max_x + 1) * (max_x + 1) <= N) max_x++;
    while (3LL * max_x * max_x > N) max_x--;

    for (int x = -max_x; x <= max_x; x++) {
        long long rem = N - 3LL * x * x;
        if (rem < 0) continue;

        int max_y = (int)sqrt((double)rem / 5.0);
        // Correct floating-point floor
        while (5LL * (max_y + 1) * (max_y + 1) <= rem) max_y++;
        while (5LL * max_y * max_y > rem) max_y--;

        count += 2 * max_y + 1;
    }

    cout << count << endl;
    return 0;
}