Composites with Prime Repunit Property
Define R(k) as the repunit of length k and A(n) as the least k such that n | R(k). For all primes p > 5, it is known that A(p) | (p-1). Find the sum of the first 25 composite values of n for which...
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\).
You are given that for all primes, \(p \gt 5\), that \(p - 1\) is divisible by \(A(p)\). For example, when \(p = 41\), \(A(41) = 5\), and \(40\) is divisible by \(5\).
However, there are rare composite values for which this is also true; the first five examples being \(91\), \(259\), \(451\), \(481\), and \(703\).
Find the sum of the first twenty-five composite values of \(n\) for which \(\gcd (n, 10) = 1\) and \(n - 1\) is divisible by \(A(n)\).
Problem 130: Composites with Prime Repunit Property
Mathematical Development
Theorem 1 (Prime repunit property). For any prime , divides .
Proof. Since is prime and , Fermat’s Little Theorem gives . Also (since ), so
Therefore , which means (since is the least such and divides any with ).
To see that whenever : if then . Since (by Theorem 2 of Problem 129, as ), the multiplicative order divides .
Theorem 2 (Fermat pseudoprime connection). If is composite with , , and (i.e., is a Fermat pseudoprime to base 10), then .
Proof. Since , we have (as shown in Problem 129). If , then , i.e., .
Lemma 1 (Divisibility of ). If , then .
Proof. Write with . Then
More directly: using and the fact that (when ), we have iff . For , the iterative computation of still yields the correct minimal period, and the divisibility property holds by the periodicity of the sequence .
Lemma 2 (Iterative computation of ). As in Problem 129: , . The first with is .
Proof. Follows from .
Editorial
This search is the composite analogue of Problem 129. The candidates must be composite and coprime to 10, so the implementation walks upward through the integers, immediately discarding multiples of 2 or 5 and discarding primes. For every remaining composite, it computes with the same repunit-remainder recurrence used in the previous problem.
The acceptance test is then straightforward: keep the number precisely when divides . The first 25 composites satisfying that condition are collected, and their sum is returned.
Pseudocode
Initialize an empty list of qualifying composites.
Scan the integers upward until 25 values have been accepted.
For each n:
Skip n if it is divisible by 2 or 5.
Skip n if it is prime.
Compute A(n) from the repunit remainder recurrence.
If A(n) divides n - 1, record n.
Return the sum of the first 25 recorded composites.
Complexity Analysis
- Time: For each candidate , computing costs , and primality testing costs . The 25 qualifying composites are found among relatively small values of (the largest is a few thousand). Total work is manageable, roughly in the worst case where is the largest qualifying composite.
- Space: beyond the result list.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n) {
if (n < 2) return false;
if (n < 4) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; (long long)i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
int repunit_order(int n) {
int r = 1;
int k = 1;
while (r != 0) {
r = (10LL * r + 1) % n;
k++;
}
return k;
}
int main() {
int count = 0;
long long total = 0;
for (int n = 2; count < 25; n++) {
if (n % 2 == 0 || n % 5 == 0) continue;
if (is_prime(n)) continue;
int an = repunit_order(n);
if ((n - 1) % an == 0) {
total += n;
count++;
}
}
cout << total << endl;
return 0;
}
"""
Problem 130: Composites with Prime Repunit Property
Find the sum of the first 25 composite n with gcd(n,10)=1
where A(n) divides (n-1).
"""
from math import gcd, isqrt
def is_prime(n):
if n < 2:
return False
if n < 4:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def repunit_order(n):
"""Find smallest k such that R(k) is divisible by n."""
r = 1
k = 1
while r != 0:
r = (10 * r + 1) % n
k += 1
return k
def solve():
composites = []
n = 2
while len(composites) < 25:
if n % 2 != 0 and n % 5 != 0 and not is_prime(n):
an = repunit_order(n)
if (n - 1) % an == 0:
composites.append(n)
n += 1
return sum(composites)
def visualize():
"""Visualize the qualifying composite numbers."""
if __name__ == "__main__":
answer = solve()
assert answer == 149253
print(answer)