All Euler problems
Project Euler

Reciprocal Cycles

Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

Source sync Apr 19, 2026
Problem #0026
Level Level 01
Solved By 93,226
Languages C++, Python
Answer 983
Length 565 words
number_theorymodular_arithmeticsimulation

Problem Statement

This archive keeps the full statement, math, and original media on the page.

A unit fraction contains \(1\) in the numerator. The decimal representation of the unit fractions with denominators \(2\) to \(10\) are given: \begin {align*} 1/2 &= 0.5\\ 1/3 &=0.(3)\\ 1/4 &=0.25\\ 1/5 &= 0.2\\ 1/6 &= 0.1(6)\\ 1/7 &= 0.(142857)\\ 1/8 &= 0.125\\ 1/9 &= 0.(1)\\ 1/10 &= 0.1 \end {align*}

Where \(0.1(6)\) means \(0.166666\cdots \), and has a \(1\)-digit recurring cycle. It can be seen that \(1/7\) has a \(6\)-digit recurring cycle.

Find the value of \(d < 1000\) for which \(1/d\) contains the longest recurring cycle in its decimal fraction part.

Problem 26: Reciprocal Cycles

Mathematical Development

Definition 1 (Multiplicative Order). Let a,nZa, n \in \mathbb{Z} with n2n \geq 2 and gcd(a,n)=1\gcd(a, n) = 1. The multiplicative order of aa modulo nn, denoted ordn(a)\operatorname{ord}_n(a), is the smallest positive integer kk such that ak1(modn)a^k \equiv 1 \pmod{n}. Existence is guaranteed by Euler’s theorem, which gives aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}.

Definition 2 (Full Reptend Prime). A prime pp with gcd(p,10)=1\gcd(p, 10) = 1 is a full reptend prime in base 10 if ordp(10)=p1\operatorname{ord}_p(10) = p - 1, i.e., 10 is a primitive root modulo pp.

Theorem 1 (Cycle Length of Unit Fractions). Let d2d \geq 2 be a positive integer. Write d=2α5βdd = 2^\alpha \cdot 5^\beta \cdot d' where gcd(d,10)=1\gcd(d', 10) = 1. Then the length of the repeating block in the decimal expansion of 1/d1/d is ordd(10)\operatorname{ord}_{d'}(10) if d>1d' > 1, and 00 if d=1d' = 1.

Proof. Consider the long division of 1÷d1 \div d. Define the remainder sequence by r0=1r_0 = 1 and rk+1=10rkmoddr_{k+1} = 10 \cdot r_k \bmod d. The decimal expansion of 1/d1/d consists of a non-repeating prefix of length s=max(α,β)s = \max(\alpha, \beta) followed by a repeating block. To see this, note that 10s(1/d)=q+1/d10^s \cdot (1/d) = q + 1/d' for some integer qq, where d=d/(2α5β)d' = d / (2^\alpha \cdot 5^\beta). Since gcd(d,10)=1\gcd(d', 10) = 1, the decimal expansion of 1/d1/d' is purely repeating. Its period is the smallest m>0m > 0 such that 10m1(modd)10^m \equiv 1 \pmod{d'}, which is precisely ordd(10)\operatorname{ord}_{d'}(10). If d=1d' = 1, then 1/d1/d terminates and the cycle length is 00. \square

Theorem 2 (Fermat’s Little Theorem). If pp is prime and gcd(a,p)=1\gcd(a, p) = 1, then ap11(modp)a^{p-1} \equiv 1 \pmod{p}.

Proof. The multiplicative group (Z/pZ)(\mathbb{Z}/p\mathbb{Z})^* has order p1p - 1. By Lagrange’s theorem, the order of every element divides the group order, so ap11(modp)a^{p-1} \equiv 1 \pmod{p}. \square

Lemma 1 (Divisibility of the Order). For a prime p10p \nmid 10, ordp(10)(p1)\operatorname{ord}_p(10) \mid (p - 1).

Proof. By Theorem 2, 10p11(modp)10^{p-1} \equiv 1 \pmod{p}. Let k=ordp(10)k = \operatorname{ord}_p(10) and write p1=qk+rp - 1 = qk + r with 0r<k0 \leq r < k. Then 10r=10p1(10k)q11q1(modp)10^r = 10^{p-1} \cdot (10^k)^{-q} \equiv 1 \cdot 1^{-q} \equiv 1 \pmod{p}. Since kk is the smallest positive integer with 10k1(modp)10^k \equiv 1 \pmod{p} and 0r<k0 \leq r < k, we must have r=0r = 0. Hence k(p1)k \mid (p - 1). \square

Theorem 3 (Maximality of Cycle Length). For composite dd with gcd(d,10)=1\gcd(d, 10) = 1, ordd(10)λ(d)ϕ(d)<d1\operatorname{ord}_d(10) \leq \lambda(d) \leq \phi(d) < d - 1, where λ\lambda denotes the Carmichael function. Consequently, the cycle length of 1/d1/d is strictly less than d1d - 1.

Proof. By definition of the Carmichael function, 10λ(d)1(modd)10^{\lambda(d)} \equiv 1 \pmod{d}, so ordd(10)λ(d)\operatorname{ord}_d(10) \mid \lambda(d), giving ordd(10)λ(d)\operatorname{ord}_d(10) \leq \lambda(d). It is well known that λ(d)ϕ(d)\lambda(d) \leq \phi(d) for all d1d \geq 1. For composite d4d \geq 4, Euler’s totient satisfies ϕ(d)dd<d1\phi(d) \leq d - \sqrt{d} < d - 1 (since dd has a non-trivial factor). Thus ordd(10)<d1\operatorname{ord}_d(10) < d - 1. \square

Corollary 1. The maximum cycle length among all d<Nd < N is achieved at a full reptend prime, i.e., a prime pp with ordp(10)=p1\operatorname{ord}_p(10) = p - 1.

Proof. By Theorem 3, composite dd yields cycle length strictly less than d1d - 1. A full reptend prime pp achieves cycle length p1p - 1. Since p1>q1ordq(10)p - 1 > q - 1 \geq \operatorname{ord}_q(10) for any q<pq < p, the largest full reptend prime below NN maximizes the cycle length, provided we verify it by checking primes in descending order. \square

Theorem 4 (Answer). The value d<1000d < 1000 that maximizes the cycle length of 1/d1/d is d=983d = 983, with cycle length 982982.

Proof. By Corollary 1, we search for the largest full reptend prime below 1000. We check primes in decreasing order:

  • p=997p = 997: We compute ord997(10)\operatorname{ord}_{997}(10) by checking divisors of 996=22383996 = 2^2 \cdot 3 \cdot 83. We find ord997(10)=166996\operatorname{ord}_{997}(10) = 166 \neq 996.
  • p=991p = 991: 990=232511990 = 2 \cdot 3^2 \cdot 5 \cdot 11. We find ord991(10)990\operatorname{ord}_{991}(10) \neq 990.
  • p=983p = 983: 982=2491982 = 2 \cdot 491 where 491491 is prime. We verify 109821(mod983)10^{982} \equiv 1 \pmod{983} (by Fermat), 10491≢1(mod983)10^{491} \not\equiv 1 \pmod{983}, and 102≢1(mod983)10^2 \not\equiv 1 \pmod{983}. Since ord983(10)\operatorname{ord}_{983}(10) divides 982982 and equals neither 11, 22, nor 491491, we conclude ord983(10)=982\operatorname{ord}_{983}(10) = 982. Thus 983983 is a full reptend prime.

Since 982>p1982 > p - 1 for every prime p<983p < 983, no smaller dd can produce a longer cycle. \square

Editorial

We test each denominator d<Nd < N separately. After removing all factors of 2 and 5, terminating decimals are skipped; otherwise we simulate long division by repeatedly updating the remainder r10rmoddr \mapsto 10r \bmod d' and recording the first step at which each remainder appears. The cycle length is the distance between repeated remainders, and the denominator with the largest such length is returned. This is sufficient because the repeating part of 1/d1/d is completely determined by this remainder sequence.

Pseudocode

Algorithm: Longest Recurring Cycle in a Unit Fraction
Require: An integer N ≥ 2.
Ensure: The denominator d < N for which 1 / d has the longest recurring decimal cycle.
1: Initialize best_d ← 0 and best_period ← 0.
2: For each denominator d in {2, 3, ..., N - 1}, remove all factors 2 and 5 to obtain d'.
3: If d' = 1, continue; otherwise simulate the remainder sequence of long division, recording the first position at which each remainder appears.
4: When a remainder repeats, compute the cycle length; if it exceeds best_period, update best_period ← cycle length and best_d ← d.
5: Return best_d.

Complexity Analysis

Proposition (Time Complexity). The algorithm runs in O(N2)O(N^2) time.

Proof. For each d{2,,N1}d \in \{2, \ldots, N-1\}, the long-division simulation computes ordd(10)\operatorname{ord}_{d'}(10) by iterating until a remainder repeats. Since the remainders lie in {0,1,,d1}\{0, 1, \ldots, d'-1\}, the loop executes at most ddd' \leq d iterations. Each iteration performs O(1)O(1) arithmetic and hash-map operations (expected). Summing over all dd: d=2N1O(d)=O(N2)\sum_{d=2}^{N-1} O(d) = O(N^2). \square

Proposition (Space Complexity). The algorithm uses O(N)O(N) auxiliary space.

Proof. The hash map seen stores at most d1<Nd' - 1 < N entries for any single invocation. It is cleared between iterations, so peak usage is O(N)O(N). \square

Answer

983\boxed{983}

Code

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

C++ project_euler/problem_026/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    int best_d = 0, best_cycle = 0;

    for (int d = 2; d < 1000; d++) {
        unordered_map<int, int> seen;
        int r = 1, pos = 0, cycle_len = 0;

        while (r != 0) {
            if (seen.count(r)) {
                cycle_len = pos - seen[r];
                break;
            }
            seen[r] = pos;
            r = (r * 10) % d;
            pos++;
        }

        if (cycle_len > best_cycle) {
            best_cycle = cycle_len;
            best_d = d;
        }
    }

    cout << best_d << endl;
    return 0;
}