### Jan

#### 2

# Length of cycles in unit fractions

Filed Under hisabati, project euler by Mucheru

Problem 26 of Project Euler asks us to find the length of cycles in unit fractions:

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

^{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 Where 0.1(6) means 0.166666…, 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.

The decimal representations of the fractions and therefore cycles can be discovered using long division. A python implementation of this would be:

1: def unit_fraction_decimal_rep(n): 2: numerator, denominator = 1, n 3: fraction = [] 4: remainders = {} 5: while True: 6: numerator *= 10 7: r = numerator % denominator 8: q = (numerator - r)/denominator 9: if(r == 0): 10: fraction.append(q) 11: break 12: if(r in remainders.values()): 13: foundCycle = False 14: for key,value in remainders.items(): 15: if(r == value) and (q == int(fraction[key])): 16: # mark the cycle with parenthesis 17: fraction.insert(key,"(") 18: fraction.append(")") 19: foundCycle = True 20: break 21: if foundCycle: 22: break 23: remainders[len(fraction)] = r 24: fraction.append(str(q)) 25: numerator = r 26: return "0."+"".join(fraction)

To solve the problem posed, we could generate fractional representations of all integers from 1000 down and check for the longest cycles. However, looking at the algorithm above we can figure out how long a cycle will be without actually having to calculate it.

The terms in the decimal representation are determined by the remainder in each iteration of the loop starting on line 5 above. If one of the remainders is 0, then the fraction terminates. On the other hand, if we see a remainder that we have seen previously, then we have gotten to the end of a cycle. The maximum length of a cycle is n-1 since there are only n-1 possibilities for the remainder.

In each iteration of the loop on line 5, we multiply the numerator by 10. This establishes a relationship between 10 and n. We use a little algebra to explore the relationship.

- If the fraction terminates, then at some point we get a remainder of 0 meaning that n evenly divides a power of 10. Since 10 has only two divisors, 2 and 5, n evenly divides a power of 10 if and only if n = 2
^{a}x5^{b}where a,b are non-negative integers (a or b may be 0 in which case n is either only divisible by 2 or by 5). - The fraction will recur if n does not evenly divide any power of 10.
- If n is has no factors in common with 10 (abbreviated as n is coprime with 10), the length of the recurring cycle is equal to the order of 10 in the group Z
_{n}(the multiplicative group of integers modulo n). If 10 has order d then 10^{d}mod n = 1 and 1 becomes the first remainder repeated (since we start with numerator 1). - If n is not coprime with 10, there is no d for which 10
^{d}mod n = 1. In such cases n= 2^{a}x5^{b}xm where m is coprime with 10. So^{1}/_{n}=^{1}/_{(2ax5b)}x^{1}/_{m}. The component^{1}/_{(2ax5b)}will terminate so the cycle that results in^{1}/_{n}is contributed by^{1}/_{m}and the length of the cycle is equal to the order of 10 in Z_{m}.

- If n is has no factors in common with 10 (abbreviated as n is coprime with 10), the length of the recurring cycle is equal to the order of 10 in the group Z

Applying this, we come up with the algorithm below:

1: def recurring_cycle_length(n): 2: cycleLen = 0 3: # remove powers of 2 and 5 in n 4: while (n % 2 == 0): 5: n /= 2 6: while (n % 5 == 0): 7: n /= 5 8: if n > 1: 9: a = 10 % n 10: cycleLen = 1 11: while a != 1: 12: a *= 10 13: a %= n 14: cycleLen += 1 15: return cycleLen

The longest cycle will be generated when 10 has a high order in Z_{n} or Z_{m}. So when searching for the longest cycle we start with the maximum value of n in the range and move down. If n is prime, the order of 10 in the Z_{n} is either n-1 or a divisor of n-1. If we find a max value for the period which is equal to n-1, we have found the longest cycle.

1: def max_len_recurring_cycle_in_range(n): 2: # iterate from the max num down 3: maxCycleLen = 0 4: maxCycleLenGenerator = n 5: for i in range(n, 1, -1): 6: currLen = recurring_cycle_length(i) 7: if(currLen > maxCycleLen): 8: maxCycleLen = currLen 9: maxCycleLenGenerator = i 10: if(currLen == i-1): 11: break 12: return maxCycleLenGenerator, maxCycleLen