All Euler problems
Project Euler

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...

Source sync May 21, 2026
Problem #0130
Level Level 05
Solved By 6,823
Languages C++, Python
Answer 149253
Length 314 words
modular_arithmeticnumber_theorybrute_force

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 p>5p > 5, A(p)A(p) divides p−1p - 1.

Proof. Since p>5p > 5 is prime and gcd⁡(p,10)=1\gcd(p, 10) = 1, Fermat’s Little Theorem gives 10p−1≡1(modp)10^{p-1} \equiv 1 \pmod{p}. Also gcd⁡(9,p)=1\gcd(9, p) = 1 (since p≠3p \neq 3), so

R(p−1)=10p−1−19≡0(modp).R(p-1) = \frac{10^{p-1} - 1}{9} \equiv 0 \pmod{p}.

Therefore p∣R(p−1)p \mid R(p-1), which means A(p)∣(p−1)A(p) \mid (p-1) (since A(p)A(p) is the least such kk and A(p)A(p) divides any kk with p∣R(k)p \mid R(k)).

To see that A(p)∣kA(p) \mid k whenever p∣R(k)p \mid R(k): if p∣R(k)p \mid R(k) then 10k≡1(modp)10^k \equiv 1 \pmod{p}. Since A(p)=ordp(10)A(p) = \text{ord}_p(10) (by Theorem 2 of Problem 129, as gcd⁡(9,p)=1\gcd(9, p) = 1), the multiplicative order divides kk. □\square

Theorem 2 (Fermat pseudoprime connection). If nn is composite with gcd⁡(n,10)=1\gcd(n, 10) = 1, gcd⁡(n,9)=1\gcd(n, 9) = 1, and 10n−1≡1(modn)10^{n-1} \equiv 1 \pmod{n} (i.e., nn is a Fermat pseudoprime to base 10), then A(n)∣(n−1)A(n) \mid (n-1).

Proof. Since gcd⁡(n,9)=1\gcd(n, 9) = 1, we have A(n)=ordn(10)A(n) = \text{ord}_n(10) (as shown in Problem 129). If 10n−1≡1(modn)10^{n-1} \equiv 1 \pmod{n}, then ordn(10)∣(n−1)\text{ord}_n(10) \mid (n-1), i.e., A(n)∣(n−1)A(n) \mid (n-1). □\square

Lemma 1 (Divisibility of AA). If n∣R(k)n \mid R(k), then A(n)∣kA(n) \mid k.

Proof. Write k=qA(n)+rk = qA(n) + r with 0≤r<A(n)0 \leq r < A(n). Then

R(k)=R(r)⋅10k−r⋅1(adjustment)+…R(k) = R(r) \cdot 10^{k-r} \cdot \frac{1}{\text{(adjustment)}} + \ldots

More directly: using R(k)=(10k−1)/9R(k) = (10^k - 1)/9 and the fact that ordn(10)=A(n)\text{ord}_n(10) = A(n) (when gcd⁡(n,9)=1\gcd(n,9) = 1), we have 10k≡1(modn)10^k \equiv 1 \pmod{n} iff A(n)∣kA(n) \mid k. For gcd⁡(n,9)≠1\gcd(n, 9) \neq 1, the iterative computation of A(n)A(n) still yields the correct minimal period, and the divisibility property holds by the periodicity of the sequence rk=R(k) mod nr_k = R(k) \bmod n. □\square

Lemma 2 (Iterative computation of A(n)A(n)). As in Problem 129: r1=1r_1 = 1, rk=(10rk−1+1) mod nr_k = (10 r_{k-1} + 1) \bmod n. The first kk with rk=0r_k = 0 is A(n)A(n).

Proof. Follows from R(k)=10R(k−1)+1R(k) = 10 R(k-1) + 1. □\square

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 A(n)A(n) with the same repunit-remainder recurrence used in the previous problem.

The acceptance test is then straightforward: keep the number precisely when A(n)A(n) divides n−1n-1. 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 nn, computing A(n)A(n) costs O(A(n))≤O(n)O(A(n)) \leq O(n), and primality testing costs O(n)O(\sqrt{n}). The 25 qualifying composites are found among relatively small values of nn (the largest is a few thousand). Total work is manageable, roughly O(Nmax⁡2)O(N_{\max}^2) in the worst case where Nmax⁡N_{\max} is the largest qualifying composite.
  • Space: O(1)O(1) beyond the result list.

Answer

149253\boxed{149253}

Code

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

C++ project_euler/problem_130/solution.cpp
#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;
}