Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.
- #Problem: Floating point representation limitation.
- #Examble: 1/19.0 = 0.05263157894736842 while
- #1/19 = 0.052631578 947368421 ; 18 repeating digits
- #Analysis:
- #Using long division to convert the fraction (rational number) into decimal form.
- #if any point the reminder is 0 its a terminating decimal.
- #if we arrive to a remainder as the one we began with, the decimal will start to repeat.
- #Source: http://en.wikipedia.org/wiki/Repeating_decimal
- #Section: Decimal expansion and recurrence sequence
- max = 1000
- maxCount = 0
- maxDivisor = 0
- for divisor in range(2,max):
- divdend = 10
- quotient = []
- reminder = []
- while True:
- if divdend < divisor:
- quotient.append(0)
- divdend *= 10
- else:
- quotient.append(divdend/divisor)
- reminder.append((divdend%divisor)*10)
- if reminder[-1] == 0:
- break
- elif len(reminder) > 1 and reminder[-1] in\
- reminder[:len(reminder)-1]:
- break
- divdend = reminder[-1]
- if reminder[-1] != 0:
- while quotient[0] == 0:
- quotient.remove(0)
- if len(quotient)-1 >= maxCount:
- maxCount = len(quotient)-1
- maxDivisor = divisor
- print maxDivisor, maxCount
Advertisement
Add Comment
Please, Sign In to add comment