All Euler problems
Project Euler

Triangular, Pentagonal, and Hexagonal

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: | Type | Formula | Sequence | |------------|----------------------------|-----------------------| | Triangle | T...

Source sync Apr 19, 2026
Problem #0045
Level Level 01
Solved By 78,367
Languages C++, Python
Answer 1533776805
Length 447 words
sequencegeometrymodular_arithmetic

Problem Statement

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

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle    
Pentagonal    
Hexagonal    

It can be verified that .

Find the next triangle number that is also pentagonal and hexagonal.

Problem 45: Triangular, Pentagonal, and Hexagonal

Mathematical Development

Mathematical Foundation

Theorem 1. Every hexagonal number is a triangular number. Specifically, Hm=T2m1H_m = T_{2m-1} for all positive integers mm.

Proof. Let Hm=m(2m1)H_m = m(2m - 1). We claim Hm=T2m1H_m = T_{2m-1}:

T2m1=(2m1)(2m1+1)2=(2m1)2m2=m(2m1)=Hm.T_{2m-1} = \frac{(2m-1)(2m-1+1)}{2} = \frac{(2m-1) \cdot 2m}{2} = m(2m-1) = H_m.

\square

Lemma 1. The converse of Theorem 1 fails: not every triangular number is hexagonal. Specifically, TnT_n is hexagonal if and only if nn is odd.

Proof. If Tn=HmT_n = H_m, then n(n+1)2=m(2m1)\frac{n(n+1)}{2} = m(2m-1), so n=2m1n = 2m - 1, which is odd. Conversely, if n=2m1n = 2m - 1 is odd, then Tn=T2m1=HmT_n = T_{2m-1} = H_m. \square

Theorem 2 (Reduction to hexagonal-pentagonal search). A number is simultaneously triangular, pentagonal, and hexagonal if and only if it is both hexagonal and pentagonal.

Proof. (\Rightarrow) Immediate.

(\Leftarrow) If vv is hexagonal, then v=Hm=T2m1v = H_m = T_{2m-1} by Theorem 1, so vv is triangular. Combined with vv being pentagonal, vv is all three. \square

Theorem 3 (Pentagonal number test). A positive integer vv is pentagonal if and only if n=1+1+24v6n = \frac{1 + \sqrt{1 + 24v}}{6} is a positive integer.

Proof. The equation Pn=vP_n = v gives n(3n1)2=v\frac{n(3n-1)}{2} = v, i.e., 3n2n2v=03n^2 - n - 2v = 0. By the quadratic formula, n=1+1+24v6n = \frac{1 + \sqrt{1 + 24v}}{6} (taking the positive root). This is a positive integer if and only if (i) 1+24v1 + 24v is a perfect square and (ii) 1+24v5(mod6)\sqrt{1 + 24v} \equiv 5 \pmod{6}. \square

Theorem 4 (Pell equation structure). The equation Hm=PkH_m = P_k, i.e., m(2m1)=k(3k1)2m(2m-1) = \frac{k(3k-1)}{2}, reduces to the generalized Pell equation 3x22y2=13x^2 - 2y^2 = 1 via the substitution x=2k13x = 2k - \frac{1}{3} (scaled) and y=2m12y = 2m - \frac{1}{2} (scaled), or more precisely, to an integer recurrence.

Proof. Setting Hm=PkH_m = P_k:

2m(2m1)=k(3k1)2m(2m - 1) = k(3k - 1) 2(2m)22(2m)=3k2k2(2m)^2 - 2(2m) = 3k^2 - k

Let u=4m1u = 4m - 1 and w=6k1w = 6k - 1. Then m=u+14m = \frac{u+1}{4} and k=w+16k = \frac{w+1}{6}. Substituting and simplifying:

2m(2m1)=(u+1)u4,k(3k1)2=(w+1)w122m(2m-1) = \frac{(u+1)u}{4}, \quad \frac{k(3k-1)}{2} = \frac{(w+1)w}{12}

The equation u(u+1)4=w(w+1)12\frac{u(u+1)}{4} = \frac{w(w+1)}{12} gives 3u(u+1)=w(w+1)3u(u+1) = w(w+1), i.e., 3u2+3u=w2+w3u^2 + 3u = w^2 + w, or equivalently 3(2u+1)2(2w+1)2=23(2u+1)^2 - (2w+1)^2 = 2, which is 3U2W2=23U^2 - W^2 = 2 with U=2u+1,W=2w+1U = 2u + 1, W = 2w + 1. This is a generalized Pell equation whose solutions form a recurrence:

(Un+1,Wn+1)=(5Un+4Wn,6Un+5Wn)(U_{n+1}, W_{n+1}) = (5U_n + 4W_n, 6U_n + 5W_n)

with initial solutions generating the sequence of hexagonal-pentagonal numbers. The solutions after (m,k)=(143,165)(m,k) = (143, 165) yield (m,k)=(27693,31977)(m,k) = (27693, 31977). \square

Theorem 5. The next number after 4075540755 that is simultaneously triangular, pentagonal, and hexagonal is 15337768051533776805.

Proof. By Theorem 2, we search hexagonal numbers HmH_m for m>143m > 143 and test pentagonality via Theorem 3. By the Pell equation structure (Theorem 4), the next solution is at m=27693m = 27693:

H27693=27693×(2×276931)=27693×55385=1533776805.H_{27693} = 27693 \times (2 \times 27693 - 1) = 27693 \times 55385 = 1533776805.

Verifying pentagonality: n=1+1+24×15337768056=1+368106433216=1+1918616=1918626=31977n = \frac{1 + \sqrt{1 + 24 \times 1533776805}}{6} = \frac{1 + \sqrt{36810643321}}{6} = \frac{1 + 191861}{6} = \frac{191862}{6} = 31977. Since P31977=31977×959302=1533776805P_{31977} = \frac{31977 \times 95930}{2} = 1533776805, the number is pentagonal.

Verifying triangularity: T55385=55385×553862=1533776805T_{55385} = \frac{55385 \times 55386}{2} = 1533776805. \square

Editorial

Since every hexagonal number is automatically triangular, it is enough to enumerate hexagonal numbers beyond the known value H143=40755H_{143} = 40755 and test only for pentagonality. Starting from m=144m = 144, we compute Hm=m(2m1)H_m = m(2m-1) and apply the discriminant criterion for pentagonal numbers. The first hexagonal number that passes this test is therefore the next number that is simultaneously triangular, pentagonal, and hexagonal.

Pseudocode

Algorithm: Next Triangular-Pentagonal-Hexagonal Number
Require: The hexagonal sequence H_m = m(2m - 1).
Ensure: The first value after 40755 that is simultaneously triangular, pentagonal, and hexagonal.
1: Initialize m ← 144.
2: Repeat:
3:     Compute H ← m(2m - 1).
4:     If H is pentagonal, return H; otherwise update m ← m + 1.

Complexity Analysis

Time (brute-force search): We iterate hexagonal numbers from m=144m = 144 to m=27693m = 27693, performing an O(1)O(1) pentagonal test at each step. Total: O(M)O(M) where M=27693M = 27693, i.e., O(27550)O(27550) iterations.

Time (Pell equation): Using the recurrence from Theorem 4, each solution is computed in O(1)O(1) from the previous one, giving O(1)O(1) per solution after finding the recurrence.

Space: O(1)O(1).

Answer

1533776805\boxed{1533776805}

Code

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

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

bool is_pentagonal(long long v) {
    if (v <= 0) return false;
    long long disc = 1 + 24 * v;
    long long s = (long long)sqrt((double)disc);
    while (s * s < disc) s++;
    while (s * s > disc) s--;
    if (s * s != disc) return false;
    return (1 + s) % 6 == 0;
}

int main() {
    // Every hexagonal number is triangular: H_m = T_{2m-1}.
    // So we just iterate hexagonal numbers and check if pentagonal.
    // Start after H_143 = 40755.
    for (long long m = 144; ; m++) {
        long long h = m * (2 * m - 1);
        if (is_pentagonal(h)) {
            cout << h << endl;
            return 0;
        }
    }
    return 0;
}