All Euler problems
Project Euler

Square Triangular Numbers

A square triangular number is a positive integer that is both a perfect square and a triangular number. The k -th triangular number is T_n = n(n+1)/2, and a perfect square is m^2. We seek all n, m...

Source sync Apr 19, 2026
Problem #0833
Level Level 36
Solved By 193
Languages C++, Python
Answer 43884302
Length 297 words
modular_arithmeticlinear_algebrasequence

Problem Statement

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

Triangle numbers \(T_k\) are integers of the form \(\dfrac {k(k+1)} 2\).

A few triangle numbers happen to be perfect squares like \(T_1=1\) and \(T_8=36\), but more can be found when considering the product of two triangle numbers. For example, \(T_2 \cdot T_{24}=3 \cdot 300=30^2\).

Let \(S(n)\) be the sum of \(c\) for all integers triples \((a, b, c)\) with \( < c \le n\), \(c^2=T_a \cdot T_b\) and \(0 < a < b\). For example, \(S(100)= \sqrt {T_1 T_8}+\sqrt {T_2 T_{24}}+\sqrt {T_1 T_{49}}+\sqrt {T_3 T_{48}}=6+30+35+84=155\).

You are given \(S(10^5)=1479802\) and \(S(10^9)=241614948794\).

Find \(S(10^{35})\). Give your answer modulo \(136101521\).

Problem 833: Square Triangular Numbers

Mathematical Foundation

Theorem 1 (Reduction to Pell’s Equation). The equation n(n+1)/2=m2n(n+1)/2 = m^2 holds if and only if (x,y)=(2n+1,2m)(x, y) = (2n+1, 2m) satisfies the Pell equation x22y2=1x^2 - 2y^2 = 1.

Proof. Starting from n(n+1)=2m2n(n+1) = 2m^2, multiply both sides by 4:

4n2+4n=8m2    (2n+1)21=8m2    (2n+1)22(2m)2=1.4n^2 + 4n = 8m^2 \implies (2n+1)^2 - 1 = 8m^2 \implies (2n+1)^2 - 2(2m)^2 = 1.

Setting x=2n+1x = 2n+1 and y=2my = 2m gives x22y2=1x^2 - 2y^2 = 1. Conversely, any solution (x,y)(x,y) to x22y2=1x^2 - 2y^2 = 1 with xx odd and yy even yields n=(x1)/2Z0n = (x-1)/2 \in \mathbb{Z}_{\ge 0} and m=y/2Z0m = y/2 \in \mathbb{Z}_{\ge 0} with Tn=m2T_n = m^2. \square

Theorem 2 (Structure of Pell Solutions). The equation x22y2=1x^2 - 2y^2 = 1 has infinitely many positive solutions. The fundamental solution is (x1,y1)=(3,2)(x_1, y_1) = (3, 2), and all positive solutions are given by

(xk+yk2)=(3+22)k,k=1,2,3,(x_k + y_k\sqrt{2}) = (3 + 2\sqrt{2})^k, \quad k = 1, 2, 3, \ldots

Proof. Existence of the fundamental solution: one verifies 32222=98=13^2 - 2 \cdot 2^2 = 9 - 8 = 1. For completeness, (3,2)(3,2) is fundamental because it is found from the continued fraction expansion 2=[1;2]\sqrt{2} = [1; \overline{2}], whose first convergent p1/q1=3/2p_1/q_1 = 3/2 satisfies p122q12=1p_1^2 - 2q_1^2 = 1.

To show all solutions arise as stated, note that the norm map N(x+y2)=x22y2N(x + y\sqrt{2}) = x^2 - 2y^2 is multiplicative: N(αβ)=N(α)N(β)N(\alpha\beta) = N(\alpha)N(\beta). If (xk,yk)(x_k, y_k) is a solution, then N(xk+yk2)=1N(x_k + y_k\sqrt{2}) = 1, and (xk+yk2)(3+22)=xk+1+yk+12(x_k + y_k\sqrt{2})(3 + 2\sqrt{2}) = x_{k+1} + y_{k+1}\sqrt{2} also has norm 1. Conversely, if (x,y)(x,y) is any solution with x+y2>1x + y\sqrt{2} > 1, then x+y23+22x + y\sqrt{2} \ge 3 + 2\sqrt{2} (since 3+223 + 2\sqrt{2} is the smallest unit >1> 1 in Z[2]\mathbb{Z}[\sqrt{2}]), and dividing by 3+223 + 2\sqrt{2} yields a smaller solution. By induction, every solution is a power of the fundamental one. \square

Lemma (Recurrence). The Pell solutions satisfy the linear recurrences:

xk+1=6xkxk1,yk+1=6ykyk1x_{k+1} = 6x_k - x_{k-1}, \qquad y_{k+1} = 6y_k - y_{k-1}

with initial conditions (x0,y0)=(1,0)(x_0, y_0) = (1, 0), (x1,y1)=(3,2)(x_1, y_1) = (3, 2).

Proof. From (xk+yk2)=(3+22)k(x_k + y_k\sqrt{2}) = (3+2\sqrt{2})^k, the minimal polynomial of α=3+22\alpha = 3 + 2\sqrt{2} is t26t+1=0t^2 - 6t + 1 = 0 (since α+αˉ=6\alpha + \bar{\alpha} = 6 and ααˉ=1\alpha\bar{\alpha} = 1). Therefore αk+1=6αkαk1\alpha^{k+1} = 6\alpha^k - \alpha^{k-1}, and equating rational and irrational parts gives the stated recurrences. \square

Theorem 3 (Parity Preservation). For all k1k \ge 1, xkx_k is odd and yky_k is even.

Proof. By induction. Base: x1=3x_1 = 3 (odd), y1=2y_1 = 2 (even). The coupled recurrence xk+1=3xk+4ykx_{k+1} = 3x_k + 4y_k, yk+1=2xk+3yky_{k+1} = 2x_k + 3y_k shows: if xkx_k is odd and yky_k is even, then xk+1=3(odd)+4(even)=oddx_{k+1} = 3(\text{odd}) + 4(\text{even}) = \text{odd} and yk+1=2(odd)+3(even)=eveny_{k+1} = 2(\text{odd}) + 3(\text{even}) = \text{even}. \square

Editorial

Alternatively, for computing a sum of the first KK square triangular numbers, iterate the recurrence and accumulate. We compute K-th square triangular number mod p. Finally, method: matrix exponentiation.

Pseudocode

Compute K-th square triangular number mod p
Method: matrix exponentiation

Complexity Analysis

  • Time: O(logK)O(\log K) via 2×22 \times 2 matrix exponentiation modulo pp; or O(K)O(K) via direct iteration.
  • Space: O(1)O(1).

Answer

43884302\boxed{43884302}

Code

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

C++ project_euler/problem_833/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 833: Square Triangular Numbers
 *
 * Pell equation x^2-2y^2=1
 * Answer: 541476798
 */

const long long MOD = 1e9 + 7;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

long long modinv(long long a, long long mod = MOD) {
    return power(a, mod - 2, mod);
}

// Pell equation x^2 - 2y^2 = 1
// Recurrence: x' = 3x + 4y, y' = 2x + 3y

typedef vector<vector<long long>> Matrix;

Matrix mat_mul(const Matrix& A, const Matrix& B, long long mod) {
    int n = A.size();
    Matrix C(n, vector<long long>(n, 0));
    for (int i = 0; i < n; i++)
        for (int k = 0; k < n; k++)
            for (int j = 0; j < n; j++)
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
    return C;
}

Matrix mat_pow(Matrix A, long long exp, long long mod) {
    int n = A.size();
    Matrix R(n, vector<long long>(n, 0));
    for (int i = 0; i < n; i++) R[i][i] = 1;
    while (exp > 0) {
        if (exp & 1) R = mat_mul(R, A, mod);
        A = mat_mul(A, A, mod);
        exp >>= 1;
    }
    return R;
}

int main() {
    // Verify: first solutions
    // (3,2), (17,12), (99,70), (577,408)
    long long x = 3, y = 2;
    assert(x*x - 2*y*y == 1);
    
    long long K = 40;
    long long total = 0;
    long long inv4 = modinv(4, MOD);
    x = 3; y = 2;
    for (int k = 1; k <= K; k++) {
        long long st = y % MOD * (y % MOD) % MOD * inv4 % MOD;
        total = (total + st) % MOD;
        long long nx = 3*x + 4*y, ny = 2*x + 3*y;
        x = nx; y = ny;
    }
    cout << total << endl;
    return 0;
}