from itertools import count def get_cycle_count(nominator, denominator): assert(nominator == 1) remainders = {} remainder = nominator results = [] for i in count(): result = remainder // denominator remainder = remainder % denominator results.append(result) if remainder in remainders: return i - remainders[remainder] else: remainders[remainder] = i if remainder == 0: return 0 remainder *= 10 def euler_026(): return max([(get_cycle_count(1, i), i) for i in range(1, 1000)])[1] if __name__ == "__main__": assert(get_cycle_count(1, 7) == 6) assert(get_cycle_count(1, 10) == 0) assert(get_cycle_count(1, 6) == 1) print("e026.py: {}".format(euler_026())) assert(euler_026() == 983)