Fractions to Decimals
fracdec
Algorithm
Simulate long division; terminate once we hit a repeated remainder
Python
from textwrap import wrap
def setio(name):
# Overwrites input/print functions to file io
global input, print
fIn = open(f"{name}.in", "r")
fOut = open(f"{name}.out", "w")
input = fIn.readline
print = lambda i: fOut.write(str(i)+"\n")
setio("fracdec")
n, d = map(int, input().split())
i = n // d
n %= d
vis = {}
res = ""
while True:
if n == 0:
if res == "":
res = "0"
break
if n in vis:
if vis[n] == 0:
res = "(" + res + ")"
else:
res = res[:vis[n]] + "(" + res[vis[n]:] + ")"
break
vis[n] = len(res)
n *= 10
res += str(n // d)
n %= d
ans = f"{i}.{res}"
print('\n'.join(wrap(ans,76)))