Reciprocal Cycles
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
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 with and . The multiplicative order of modulo , denoted , is the smallest positive integer such that . Existence is guaranteed by Euler’s theorem, which gives .
Definition 2 (Full Reptend Prime). A prime with is a full reptend prime in base 10 if , i.e., 10 is a primitive root modulo .
Theorem 1 (Cycle Length of Unit Fractions). Let be a positive integer. Write where . Then the length of the repeating block in the decimal expansion of is if , and if .
Proof. Consider the long division of . Define the remainder sequence by and . The decimal expansion of consists of a non-repeating prefix of length followed by a repeating block. To see this, note that for some integer , where . Since , the decimal expansion of is purely repeating. Its period is the smallest such that , which is precisely . If , then terminates and the cycle length is .
Theorem 2 (Fermat’s Little Theorem). If is prime and , then .
Proof. The multiplicative group has order . By Lagrange’s theorem, the order of every element divides the group order, so .
Lemma 1 (Divisibility of the Order). For a prime , .
Proof. By Theorem 2, . Let and write with . Then . Since is the smallest positive integer with and , we must have . Hence .
Theorem 3 (Maximality of Cycle Length). For composite with , , where denotes the Carmichael function. Consequently, the cycle length of is strictly less than .
Proof. By definition of the Carmichael function, , so , giving . It is well known that for all . For composite , Euler’s totient satisfies (since has a non-trivial factor). Thus .
Corollary 1. The maximum cycle length among all is achieved at a full reptend prime, i.e., a prime with .
Proof. By Theorem 3, composite yields cycle length strictly less than . A full reptend prime achieves cycle length . Since for any , the largest full reptend prime below maximizes the cycle length, provided we verify it by checking primes in descending order.
Theorem 4 (Answer). The value that maximizes the cycle length of is , with cycle length .
Proof. By Corollary 1, we search for the largest full reptend prime below 1000. We check primes in decreasing order:
- : We compute by checking divisors of . We find .
- : . We find .
- : where is prime. We verify (by Fermat), , and . Since divides and equals neither , , nor , we conclude . Thus is a full reptend prime.
Since for every prime , no smaller can produce a longer cycle.
Editorial
We test each denominator separately. After removing all factors of 2 and 5, terminating decimals are skipped; otherwise we simulate long division by repeatedly updating the remainder 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 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 time.
Proof. For each , the long-division simulation computes by iterating until a remainder repeats. Since the remainders lie in , the loop executes at most iterations. Each iteration performs arithmetic and hash-map operations (expected). Summing over all : .
Proposition (Space Complexity). The algorithm uses auxiliary space.
Proof. The hash map seen stores at most entries for any single invocation. It is cleared between iterations, so peak usage is .
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 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;
}
def cycle_length(d):
seen = {}
r, pos = 1, 0
while r != 0:
if r in seen:
return pos - seen[r]
seen[r] = pos
r = (r * 10) % d
pos += 1
return 0
def solve():
best_d, best_len = 0, 0
for d in range(2, 1000):
cl = cycle_length(d)
if cl > best_len:
best_len = cl
best_d = d
print(best_d)
if __name__ == "__main__":
solve()