All Euler problems
Project Euler

47-smooth Triangular Numbers

A positive integer m is called p -smooth if every prime factor of m is at most p. Let T(n) = tfracn(n+1)2 denote the n -th triangular number. Determine S = sum_(n >= 1, T(n) is 47-smooth) n.

Source sync Apr 19, 2026
Problem #0581
Level Level 15
Solved By 1,042
Languages C++, Python
Answer 2227616372734
Length 476 words
number_theorylinear_algebrabrute_force

Problem Statement

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

A number is \(p\)-smooth if it has no prime factors larger than \(p\).

Let \(T\) be the sequence of triangular numbers, i.e. \(T(n)=n(n+1)/2\).

Find the sum of all indices \(n\) such that \(T(n)\) is \(47\)-smooth.

Problem 581: 47-smooth Triangular Numbers

Notation and Definitions

Definition 1. Let P={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}\mathcal{P} = \{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47\} denote the set of the first 15 primes. A positive integer mm is 47-smooth (equivalently, P\mathcal{P}-smooth) if and only if rad(m)pPp\operatorname{rad}(m) \mid \prod_{p \in \mathcal{P}} p, where rad(m)=pmp\operatorname{rad}(m) = \prod_{p \mid m} p is the radical of mm.

Mathematical Foundation

Theorem 1 (Reduction to Consecutive Smooth Pairs). For every positive integer nn, the triangular number T(n)=n(n+1)2T(n) = \tfrac{n(n+1)}{2} is 47-smooth if and only if both nn and n+1n+1 are 47-smooth.

Proof. Since gcd(n,n+1)=1\gcd(n, n+1) = 1, the integers nn and n+1n+1 share no common prime factor. Exactly one of them is even; denote the even one by ee and the odd one by oo. Then

T(n)=n(n+1)2=e2o.T(n) = \frac{n(n+1)}{2} = \frac{e}{2} \cdot o.

Since gcd(e/2,o)=1\gcd(e/2, o) = 1 (as e/2e/2 and oo share no prime factor), the prime factorization of T(n)T(n) is the multiset union of the prime factorizations of e/2e/2 and oo.

()(\Rightarrow) Suppose T(n)T(n) is 47-smooth. Every prime pp dividing oo also divides T(n)T(n), so p47p \leq 47. Every prime pp dividing e/2e/2 also divides T(n)T(n), so p47p \leq 47. Since e=2(e/2)e = 2 \cdot (e/2), the only additional prime factor of ee beyond those of e/2e/2 is 2P2 \in \mathcal{P}. Hence both nn and n+1n+1 are 47-smooth.

()(\Leftarrow) Suppose both nn and n+1n+1 are 47-smooth. Then every prime dividing n(n+1)n(n+1) lies in P\mathcal{P}. Since T(n)=n(n+1)/2T(n) = n(n+1)/2 and division by 2 removes at most one factor of 2 (which cannot introduce new primes), T(n)T(n) is 47-smooth. \blacksquare

Theorem 2 (Finiteness via Stormer’s Theorem). For any finite set of primes P\mathcal{P}, there exist only finitely many positive integers nn such that both nn and n+1n+1 are P\mathcal{P}-smooth.

Proof. This is a classical result due to Stormer (1897). We sketch the argument. Given consecutive P\mathcal{P}-smooth integers nn and n+1n+1, set x=2n+1x = 2n + 1 so that

x2=4n(n+1)+1.x^2 = 4n(n+1) + 1.

Let dd be the squarefree part of n(n+1)n(n+1). Since n(n+1)n(n+1) is P\mathcal{P}-smooth, dd is a squarefree P\mathcal{P}-smooth number. Write n(n+1)=dy2n(n+1) = d y^2 for some positive integer yy. Then x24dy2=1x^2 - 4dy^2 = 1, which is a Pell equation in (x,2y)(x, 2y) with parameter dd.

There are only finitely many squarefree P\mathcal{P}-smooth numbers (precisely 2P2^{|\mathcal{P}|}). For each such dd, the Pell equation X2dY2=1X^2 - dY^2 = 1 has solutions forming a single recursive sequence growing exponentially. For sufficiently large solutions, n=(x1)/2n = (x-1)/2 will have a prime factor exceeding max(P)\max(\mathcal{P}). Hence each Pell equation contributes at most finitely many valid pairs, and the total count is finite. \blacksquare

Lemma 1 (Explicit Upper Bound). Every consecutive pair (n,n+1)(n, n+1) of 47-smooth integers satisfies n<1.2×1012n < 1.2 \times 10^{12}.

Proof. By exhaustive application of Stormer’s method — enumerating all Pell equation fundamental solutions for each of the 2152^{15} squarefree 47-smooth numbers and tracking which solutions yield consecutive smooth pairs — one verifies that the largest such pair is (n,n+1)=(1109496723124,  1109496723125)(n, n+1) = (1\,109\,496\,723\,124,\; 1\,109\,496\,723\,125), which satisfies n<1.2×1012n < 1.2 \times 10^{12}. \blacksquare

Editorial

T(n) = n(n+1)/2 is 47-smooth iff both n and n+1 are 47-smooth (Theorem 1). By Stormer’s theorem, there are finitely many such n, all below 1.2e12. Algorithm: enumerate all 47-smooth numbers up to the bound via min-heap, then identify consecutive pairs (n, n+1) and sum the values of n.

Pseudocode

while heap is non-empty
for p in P

Complexity Analysis

Let S=Ψ(L,47)S = \Psi(L, 47) denote the count of 47-smooth integers up to LL. By the Dickman—de Bruijn estimate, Ψ(L,B)Lρ(u)\Psi(L, B) \approx L \cdot \rho(u) where u=logLlogBu = \frac{\log L}{\log B} and ρ\rho is the Dickman function. Here ulog(1.2×1012)log477.2u \approx \frac{\log(1.2 \times 10^{12})}{\log 47} \approx 7.2, giving SS on the order of millions.

  • Time: O(SPlogS)O(S \cdot |\mathcal{P}| \cdot \log S). Each of the SS extractions involves a heap operation of cost O(logS)O(\log S) and spawns at most P=15|\mathcal{P}| = 15 insertions.
  • Space: O(S)O(S) for the heap and the visited set.

Answer

2227616372734\boxed{2227616372734}

Code

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

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

int main(){
    // Find sum of all n such that T(n)=n(n+1)/2 is 47-smooth
    // Equivalently, find consecutive 47-smooth numbers (n, n+1) and sum all n

    const vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
    const long long LIMIT = 1200000000000LL; // Upper bound from Stormer's theorem

    // Generate all 47-smooth numbers up to LIMIT using a min-heap
    priority_queue<long long, vector<long long>, greater<long long>> pq;
    set<long long> visited;
    pq.push(1);
    visited.insert(1);

    long long prev = -1;
    long long total = 0;

    while(!pq.empty()){
        long long v = pq.top();
        pq.pop();

        if(v > LIMIT) break;

        // Check if v and prev are consecutive
        if(v == prev + 1 && prev >= 1){
            total += prev; // n = prev, n+1 = v
        }

        prev = v;

        for(int p : primes){
            if(v <= LIMIT / p){ // overflow check
                long long nv = v * p;
                if(visited.find(nv) == visited.end()){
                    visited.insert(nv);
                    pq.push(nv);
                }
            }
        }
    }

    cout << total << endl;
    return 0;
}