#Skip to menu

Reciprocal cycles

First read the problem description.
def cycle_length(d):
    n = 1
    numerators = []

    while True:
        n *= 10
        try:
            cycle_start = numerators.index(n)
            return len(numerators[cycle_start:])
        except ValueError:
            numerators.append(n)
            n = n % d
            if n == 0:
                return 0

longest_length = 0
longest_d = None

for d in range(2, 1000):
    l = cycle_length(d)
    if l > longest_length:
        longest_length = l
        longest_d = d

Source code of the solution(s):