All Euler problems
Project Euler

Strong Repunits

A positive integer n is a repunit in base b if its base- b representation consists entirely of 1s, i.e., n = sum_(i=0)^(k-1) b^i for some k >= 2. A positive integer is a strong repunit if it is a r...

Source sync May 21, 2026
Problem #0346
Level Level 06
Solved By 5,066
Languages C++, Python
Answer 336108797689259276
Length 269 words
brute_forcearithmetic

Problem Statement

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

The number \(7\) is special, because \(7\) is \(111\) written in base \(2\), and \(11\) written in base \(6\) (i.e. \(7_{10} = 11_6 = 111_2\)). In other words, \(7\) is a repunit in at least two bases \(b > 1\).

We shall call a positive integer with this property a strong repunit. It can be verified that there are \(8\) strong repunits below \(50\): \(\{1,7,13,15,21,31,40,43\}\).

Furthermore, the sum of all strong repunits below \(1000\) equals \(15864\).

Find the sum of all strong repunits below \(10^{12}\).

Problem 346: Strong Repunits

Mathematical Foundation

Theorem 1 (Repunit Formula). The repunit with kk digits in base bb is

R(b,k)=∑i=0k−1bi=bk−1b−1.R(b,k) = \sum_{i=0}^{k-1} b^i = \frac{b^k - 1}{b-1}.

Proof. This is the standard geometric series formula: ∑i=0k−1bi=bk−1b−1\sum_{i=0}^{k-1} b^i = \frac{b^k - 1}{b - 1} for b≠1b \ne 1. □\square

Theorem 2 (Trivial Repunit Property). Every integer n≥2n \ge 2 is the repunit 11⏟2 digits\underbrace{11}_{2\text{ digits}} in base b=n−1b = n - 1, since 1⋅(n−1)+1=n1 \cdot (n-1) + 1 = n.

Proof. R(n−1,2)=1+(n−1)=nR(n-1, 2) = 1 + (n-1) = n. □\square

Lemma 1 (Strong Repunit Characterization). An integer n≥2n \ge 2 is a strong repunit if and only if nn is a repunit with k≥3k \ge 3 digits in at least one base b≥2b \ge 2. The number 1 is also a strong repunit (it is “1” in every base, and R(b,1)=1R(b,1) = 1 for all bb).

Proof. By Theorem 2, every n≥2n \ge 2 is already a 2-digit repunit in base n−1n-1. So nn is a strong repunit iff it is a repunit in at least one additional base, which means it must be a repunit with k≥3k \ge 3 digits in some base b≥2b \ge 2 (a 2-digit repunit in a different base would also suffice, but R(b,2)=b+1R(b,2) = b+1 and being equal to R(b′,2)=b′+1R(b',2) = b'+1 for b≠b′b \ne b' is impossible). For n=1n = 1: R(b,1)=1R(b,1) = 1 for all b≥2b \ge 2, so 1 qualifies trivially. □\square

Lemma 2 (Enumeration Bounds). For k≥3k \ge 3 and R(b,k)<L=1012R(b,k) < L = 10^{12}:

  • For k=3k = 3: b<L≈106b < \sqrt{L} \approx 10^6.
  • For k=40k = 40: R(2,40)=240−1≈1.1×1012R(2, 40) = 2^{40} - 1 \approx 1.1 \times 10^{12}, so k≤40k \le 40 suffices.
  • The total number of (b,k)(b,k) pairs is O ⁣(∑k=340L1/(k−1))=O(106)O\!\left(\sum_{k=3}^{40} L^{1/(k-1)}\right) = O(10^6).

Proof. R(b,k)≥bk−1R(b,k) \ge b^{k-1}, so b≤L1/(k−1)b \le L^{1/(k-1)}. For k=3k = 3, b≤L1/2=106b \le L^{1/2} = 10^6. For k=40k = 40, b≤L1/39<3b \le L^{1/39} < 3. Summing gives a geometric-like series dominated by the k=3k=3 term. □\square

Editorial

We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

    repunits = empty set

    For k from 3 to 40:
        For b from 2 to ...:
            r = (b^k - 1) / (b - 1)
            If r >= L then stop this loop
            repunits.add(r)

    repunits.add(1) // 1 is a strong repunit
    Return sum(repunits)

Complexity Analysis

  • Time: O ⁣(∑k=340L1/(k−1))=O(106)O\!\left(\sum_{k=3}^{40} L^{1/(k-1)}\right) = O(10^6) for enumeration plus O(∣set∣log⁡∣set∣)O(|\text{set}| \log |\text{set}|) for deduplication. Total: O(106)O(10^6).
  • Space: O(∣set∣)=O(106)O(|\text{set}|) = O(10^6) for storing distinct repunits.

Answer

336108797689259276\boxed{336108797689259276}

Code

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

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

int main() {
    const long long LIMIT = 1000000000000LL; // 10^12
    set<long long> repunits;

    // 1 is a repunit in every base
    repunits.insert(1);

    // For each number of digits k >= 3, enumerate repunits in base b >= 2
    // Every n >= 2 is "11" in base n-1, so any repunit with k>=3 digits
    // is automatically a strong repunit (repunit in at least 2 bases).
    for (int k = 3; k <= 40; k++) {
        for (long long b = 2; ; b++) {
            // Compute 1 + b + b^2 + ... + b^(k-1)
            long long val = 0;
            long long power = 1;
            bool overflow = false;
            for (int i = 0; i < k; i++) {
                val += power;
                if (val >= LIMIT) { overflow = true; break; }
                if (i < k - 1) {
                    // Check overflow before multiplying
                    if (power > LIMIT / b) { overflow = true; break; }
                    power *= b;
                }
            }
            if (overflow || val >= LIMIT) break;
            repunits.insert(val);
        }
    }

    long long sum = 0;
    for (long long v : repunits) {
        sum += v;
    }

    cout << sum << endl;
    return 0;
}