Repunit Divisibility
A number consisting entirely of ones is called a repunit. Define R(k) = underbrace11ldots1_k. Given that n is a positive integer and gcd(n, 10) = 1, there always exists k such that n | R(k). Let A(...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A number consisting entirely of ones is called a repunit. We shall define \(R(k)\) to be a repunit of length \(k\); for example, \(R(6) = 111111\).
Given that \(n\) is a positive integer and \(\gcd (n, 10) = 1\), it can be shown that there always exists a value, \(k\), for which \(R(k)\) is divisible by \(n\), and let \(A(n)\) be the least such value of \(k\); for example, \(A(7) = 6\) and \(A(41) = 5\).
The least value of \(n\) for which \(A(n)\) first exceeds ten is \(17\).
Find the least value of \(n\) for which \(A(n)\) first exceeds one-million.
Problem 129: Repunit Divisibility
Mathematical Development
Definition. The repunit of length is .
Theorem 1 (Existence of ). For any positive integer with , exists and .
Proof. Consider the sequence for . This sequence takes values in .
Case 1: If for some , then and .
Case 2: If for all , then by the pigeonhole principle, there exist with . Now:
Since and (because ), we get , where . Thus .
Theorem 2 (Connection to multiplicative order). Let denote the multiplicative order of 10 modulo . Then:
- If : .
- If : .
- If : (when defined appropriately via the recurrence).
In general, , but it is simplest to compute directly.
Proof. . If , then , so . The cases where require analyzing the interaction of factors of 3 in and in , but the iterative computation handles all cases uniformly.
Lemma 1 (Iterative computation). can be computed via the recurrence , , and is the smallest with .
Proof. We have and . Working modulo preserves the recurrence: . The first with is exactly .
Theorem 3 (Search bound). Since (Theorem 1) and we need , any solution must satisfy .
Proof. Immediate from .
Editorial
Because Theorem 1 guarantees , any value with must itself lie above . That means the search can begin immediately at . There is also no reason to test numbers divisible by 2 or 5, since those are not coprime to 10 and therefore do not admit an at all.
For each admissible candidate, the implementation computes directly from the repunit remainder recurrence. Starting from the remainder of , it repeatedly appends one more digit 1 modulo until the remainder becomes zero. The first candidate above one million whose period exceeds one million is the answer.
Pseudocode
Start from n = 10^6 + 1 and increase n one by one.
For each candidate n:
Skip it if n shares a factor with 10.
Compute A(n) by tracking the remainders of successive repunits modulo n:
Begin with the remainder of R(1).
Repeatedly append one more digit 1 modulo n until the remainder becomes 0.
Count how many steps this takes.
If A(n) is greater than 10^6, return n.
Complexity Analysis
- Time: For each candidate , computing takes steps. The number of candidates checked before finding the answer is small (approximately 23, since the answer is ). Total: .
- Space: — only a constant number of variables.
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 A(int n) {
// Find smallest k such that R(k) % n == 0
// R(k) = (10*R(k-1) + 1)
int r = 1;
int k = 1;
while (r != 0) {
r = (10LL * r + 1) % n;
k++;
}
return k;
}
int main() {
int limit = 1000000;
for (int n = limit + 1; ; n++) {
if (n % 2 == 0 || n % 5 == 0) continue;
if (A(n) > limit) {
cout << n << endl;
return 0;
}
}
return 0;
}
"""
Problem 129: Repunit Divisibility
Find the least n > 10^6 such that A(n) > 10^6,
where A(n) is the smallest k for which R(k) is divisible by n.
"""
def repunit_order(n):
"""Find smallest k such that R(k) divisible by n."""
r = 1
k = 1
while r != 0:
r = (10 * r + 1) % n
k += 1
return k
def solve():
limit = 10**6
n = limit + 1
while True:
if n % 2 != 0 and n % 5 != 0:
if repunit_order(n) > limit:
return n
n += 1
def visualize():
"""Visualize A(n) for values near the answer."""
if __name__ == "__main__":
answer = solve()
assert answer == 1000023
print(answer)