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...
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 digits in base is
Proof. This is the standard geometric series formula: for .
Theorem 2 (Trivial Repunit Property). Every integer is the repunit in base , since .
Proof. .
Lemma 1 (Strong Repunit Characterization). An integer is a strong repunit if and only if is a repunit with digits in at least one base . The number 1 is also a strong repunit (it is “1” in every base, and for all ).
Proof. By Theorem 2, every is already a 2-digit repunit in base . So is a strong repunit iff it is a repunit in at least one additional base, which means it must be a repunit with digits in some base (a 2-digit repunit in a different base would also suffice, but and being equal to for is impossible). For : for all , so 1 qualifies trivially.
Lemma 2 (Enumeration Bounds). For and :
- For : .
- For : , so suffices.
- The total number of pairs is .
Proof. , so . For , . For , . Summing gives a geometric-like series dominated by the term.
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: for enumeration plus for deduplication. Total: .
- Space: for storing distinct repunits.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
def solve():
LIMIT = 10**12
repunits = set()
repunits.add(1)
# Enumerate repunits with k >= 3 digits in base b >= 2
# Each such number is also "11" in base (n-1), making it a strong repunit
for k in range(3, 41):
b = 2
while True:
# Compute 1 + b + b^2 + ... + b^(k-1)
val = (b**k - 1) // (b - 1)
if val >= LIMIT:
break
repunits.add(val)
b += 1
print(sum(repunits))
if __name__ == "__main__":
solve()