Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Repeating Decimals by Sim0000
def convert(n, d):
ip, n = divmod(n, d)
x = ''
r = []
while n not in r:
r.append(n)
q, n = divmod(10 * n, d)
x += str(q)
ix = r.index(n)
return '{}.{}{}'.format(ip, x[:ix], '('+x[ix:]+')' if x[ix:] != '0' else '')
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert convert(1, 3) == "0.(3)", "1/3 Classic"
assert convert(5, 3) == "1.(6)", "5/3 The same, but bigger"
assert convert(3, 8) == "0.375", "3/8 without repeating part"
assert convert(7, 11) == "0.(63)", "7/11 prime/prime"
assert convert(29, 12) == "2.41(6)", "29/12 not and repeating part"
assert convert(11, 7) == "1.(571428)", "11/7 six digits"
assert convert(0, 117) == "0", "Zero"
June 10, 2014