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 Apr 19, 2026
Problem #0130
Level Level 05
Solved By 6,823
Languages C++, Python
Answer 149253
Length 263 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 Foundation

Theorem 1 (Prime repunit property). For any prime p>5p > 5, A(p)A(p) divides p1p - 1.

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

R(p1)=10p1190(modp).R(p-1) = \frac{10^{p-1} - 1}{9} \equiv 0 \pmod{p}.

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

To see that A(p)kA(p) \mid k whenever pR(k)p \mid R(k): if pR(k)p \mid R(k) then 10k1(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 10n11(modn)10^{n-1} \equiv 1 \pmod{n} (i.e., nn is a Fermat pseudoprime to base 10), then A(n)(n1)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 10n11(modn)10^{n-1} \equiv 1 \pmod{n}, then ordn(10)(n1)\text{ord}_n(10) \mid (n-1), i.e., A(n)(n1)A(n) \mid (n-1). \square

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

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

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

More directly: using R(k)=(10k1)/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 10k1(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)modnr_k = R(k) \bmod n. \square

Lemma 2 (Iterative computation of A(n)A(n)). As in Problem 129: r1=1r_1 = 1, rk=(10rk1+1)modnr_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(k1)+1R(k) = 10 R(k-1) + 1. \square

Editorial

We compute A(n). We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.

Pseudocode

    results = []
    n = 1
    While len(results) < target_count:
        n += 1
        If gcd(n, 10) != 1 then
            continue
        If is_prime(n) then
            continue
        Compute A(n)
        r = 1 mod n
        k = 1
        While r != 0:
            r = (10 * r + 1) mod n
            k += 1
        A_n = k
        If (n - 1) mod A_n == 0 then
            results.append(n)
    Return sum(results)

    if n < 2: return false
    for d = 2, 3, ..., floor(sqrt(n)):
        if n mod d == 0: return false
    Return true

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(Nmax2)O(N_{\max}^2) in the worst case where NmaxN_{\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;
}