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 Foundation
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
We compute A(n). We then check after loop: need to handle k=1 case (r=1 != 0 for n>1). Finally, actually iterate: r starts at 1 (=R(1)), check if 0, then update.
Pseudocode
Compute A(n)
Check after loop: need to handle k=1 case (r=1 != 0 for n>1)
Actually iterate: r starts at 1 (=R(1)), check if 0, then update
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."""