Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import msvcrt
- def find_repeating_decimals(numerator, denominator, cycle_count):
- count = 0
- remainders = set()
- while count < cycle_count:
- if numerator < denominator:
- numerator *= 10
- else:
- pass
- numerator = numerator % denominator
- if numerator in remainders:
- return(denominator, count)
- else:
- remainders.add(numerator)
- count += 1
- if __name__ == '__main__':
- abort = False
- while not abort:
- if msvcrt.kbhit() and msvcrt.getch().decode() == chr(27): # If ESC
- abort = True
- else:
- longest = [0, 0]
- try:
- numerator = eval(input('Enter your numerator: '))
- denominator = eval(input('Enter your denominator: '))
- for x in range(2, denominator + 1):
- y = find_repeating_decimals(1, x, x)
- if y[1] > longest[1]:
- longest = y
- string = str(numerator / denominator)
- remainder = numerator % denominator
- if '0.' in string:
- print('\n0.({})\n'.format(string[2:longest[1]+2]))
- else:
- print('\n', string[:-2], '\n')
- except:
- print('\nYou have not entered valid input\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement