Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import msvcrt
  2.  
  3.  
  4. def find_repeating_decimals(numerator, denominator, cycle_count):
  5. count = 0
  6. remainders = set()
  7. while count < cycle_count:
  8. if numerator < denominator:
  9. numerator *= 10
  10. else:
  11. pass
  12. numerator = numerator % denominator
  13. if numerator in remainders:
  14. return(denominator, count)
  15. else:
  16. remainders.add(numerator)
  17. count += 1
  18.  
  19.  
  20. if __name__ == '__main__':
  21. abort = False
  22. while not abort:
  23. if msvcrt.kbhit() and msvcrt.getch().decode() == chr(27): # If ESC
  24. abort = True
  25. else:
  26. longest = [0, 0]
  27. try:
  28. numerator = eval(input('Enter your numerator: '))
  29. denominator = eval(input('Enter your denominator: '))
  30. for x in range(2, denominator + 1):
  31. y = find_repeating_decimals(1, x, x)
  32. if y[1] > longest[1]:
  33. longest = y
  34. string = str(numerator / denominator)
  35. remainder = numerator % denominator
  36. if '0.' in string:
  37. print('\n0.({})\n'.format(string[2:longest[1]+2]))
  38. else:
  39. print('\n', string[:-2], '\n')
  40. except:
  41. print('\nYou have not entered valid input\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement