def ftd(n, d): intPart=n/d remain=(n%d)*10 decPart=[ ] dix={ } i=0 while True: dec=remain/d remain=remain%d decPart+=dec, if remain==0: break if remain in dix: decPart.insert( dix[remain]+1, '(') decPart+=')' break dix[remain]=i i+=1 remain*=10 return str(intPart)+'.'+''.join(map(str, decPart))