def my_square(n): x = 1.0 while 1: if x * x >= n: x=(x * x + (x - 1) * (x - 1)) / 2 for i in range(0,10): j = n / x x = (j + x) / 2 print '%.30f' % x break else: x = x + 1
↑小数点下30桁まで計算できる
↓誤差が出ないバージョン。小数点以下100桁まで。
UNTIL = 100
def f(n):
    xn = 0
    while xn * xn < n:
        xn += 1
    return len(str(xn))
def g(s, p):
    return s[:p] + '.' + s[p:]
def sq(n):
    a, b = 5*n, 5
    res = ''
    sz = 0
    cnt = 0
    while sz <= UNTIL:
        if a >= b:
            a, b = a-b, b+10
            cnt += 1
        else:
            a, b = a*100, (b/10)*100 + b%10
            res += str(cnt)
            sz += 1
            cnt = 0
    return g(res, f(n))
print sq(2)
print sq(10000)