Always has infinitely many integer solutions, generated from a single fundamental solution.

Continued fractions — the key tool

Every quadratic irrational has a periodic continued fraction expansion:

and the fundamental solution appears among its convergents .

// continued fraction expansion of sqrt(D)
vector<long long> cfSqrt(long long D) {
    long long a0 = (long long)sqrtl((long double)D);
    if (a0 * a0 == D) return {};                          // perfect square: no solution
    vector<long long> cf{a0};
    long long m = 0, d = 1, a = a0;
    while (a != 2 * a0) {
        m = d * a - m;
        d = (D - m * m) / d;
        a = (a0 + m) / d;
        cf.push_back(a);
    }
    return cf;                                            // period ends when a == 2*a0
}

The recurrence keeps everything in integers — no floating point beyond the initial .

The fundamental solution

Generate convergents until :

pair<long long,long long> pellFundamental(long long D) {
    vector<long long> cf = cfSqrt(D);
    long long p0 = 1, q0 = 0, p1 = cf[0], q1 = 1;
    for (int i = 1; ; i++) {
        if (p1 * p1 - D * q1 * q1 == 1) return {p1, q1};
        long long a = cf[i % (cf.size() - 1) + (i >= (int)cf.size() ? 1 : 0)];
        // (index into the periodic part; simplest is to extend cf lazily)
        long long p2 = a * p1 + p0, q2 = a * q1 + q0;
        p0 = p1; q0 = q1; p1 = p2; q1 = q2;
    }
}

The numbers explode

The fundamental solution can be enormous. For it is ; for , has 30 digits. Use __int128 or big integers, and be suspicious of any problem that asks for up to without a modulus.

Generating all solutions

If is fundamental, then

which unrolls to the recurrence

For the -th solution modulo something, this is a matrix power:

This is how “find the -th solution mod ” problems are solved.

The negative Pell equation

is solvable iff the continued fraction period of has odd length. In that case the fundamental solution is the convergent at the end of the first period, and squaring it gives the fundamental solution of the positive equation.

Necessary (not sufficient) condition: has no prime factor .

Generalised Pell:

Harder. The solutions fall into finitely many classes, each generated by multiplying a fundamental class solution by powers of the Pell unit. The LMM (Lagrange-Matthews-Mollin) algorithm enumerates the classes. Rarely needed in contests.

Where it appears

ProblemConnection
”Smallest with directly
Square triangular numbers
Almost-equilateral Heronian trianglesPell-type
reduces to Pell
Best rational approximations to the convergents themselves
Nearest integer to conjugate trick: the sum with its conjugate is an integer
Fibonacci-like recurrences from the same algebraic-number machinery

The conjugate trick

Because , the quantity is an integer and the second term is tiny. So

for appropriate parity. The integer satisfies a linear recurrence, so it is computable by matrix exponentiation — the standard way to handle “floor of an irrational power” problems.

See also: Euclidean Algorithm · Matrix Exponentiation · Linear Diophantine Equations