Jackpotjoy

## Project Euler Problem 26

### Question

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 $\lt$1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

### Analysis:

To find a recurring cycle is key to this problem , Let try 7 as an example. according to divide process , if reminder,equals to he original denominator  multiple 10 ,then that signals a recurring cycle . for n < 1000,  denominator has 3 cases ,10, 100, and 1000.

### Python Code:

def get_longest(n):
de = None
if n < 1000 and n > 100:
de = 1000
elif n < 100 and n >10:
de = 100
else:
de = 10
if n % 2 == 0 or n % 5 == 0:
return (n,1)
else:
reminder = de % n
c = 1
while reminder * 10 != de:
reminder = reminder  * 10 % n
c += 1
return (n,c)

print max([ get_longest(i) for i in range(2,1000) ],key = lambda x: x[1])