from sys import *
from math import *

# Use: python sqrt1.py n evaluates sqrt(n) to 1000 figures by a method 
# similar to that used in the EDSAC program.  Numbers are either single 
# 'digit's in base 10^10 or fractions in base 10^10.  To emphasise that
# the 'digit's are symbols not numbers they are stored as strings of 10
# decimal digits, so that counting in base10^10 goes "0000000000",
# "0000000001", "0000000002" ... "9999999999".
# The program has been tested only for n=2,3 and 5.

def add(a, b, c):
   "evaluates sum of digits 'a', 'b', 'c'"
   d=str(long(a) + long(b) + long(c))
   d=d.zfill(2*M)
   return (d[0:M], d[M:2*M])

def longadd(Sum, term):
    "evaluates addition of lists of digits"
    carry=Zero
    for i in range(L-1, -1, -1):
        (carry, sum)=add(Sum[i], term[i], carry)
        Sum[i]=sum
    return Sum

def times(a, b, c):
    "evaluates a times b plus c"
    d=str(long(a)*long(b) + long(c))
    d=d.zfill(2*M)
    return (d[0:M], d[M:2*M])

def longmult(term, z):
    "evaluates term times a single 'digit'"
    carry=Zero
    for i in range(L-1,-1,-1):
        (carry, prod)=times(term[i], z, carry)
        term[i]=prod
    return term

def divide(a, b, c):
    "returns quotient and remainder in dividing intergers"
    (quot,rem)=divmod(long(a) + N*long(b), long(c))
    return (str(quot), str(rem))

def longdiv(term, z):
    "evaluates term divided by integer z"
    rem=Zero
    for i in range(0, L):
        (quot, rem)=divide(term[i], rem, z)
        term[i]=quot.zfill(M)
    return term

# number of decimal digits is L*M
L=121
# length of string 'digit'
M=10
# zero
Zero='0'*M
# maximum number with of M decimal digits
N=10000000000L
term=[Zero for i in range(0,L)]
Sum =[Zero for i in range(0,L)]

# calculate square root of n
n=int(argv[1])
#real numbers are expressed by scaling by 1e10 and using long integers 
s=long(N*sqrt(n)/n)
# round s so that last few digits are zero
s=1000L*(s/1000L)
x=n*s
# note the following division is exact
eps=(N*N*n - x*x)/n

# factorize eps
m=1
while (eps%10) == 0:
    eps=eps/10
    m=m*10
eps1=str(eps)
eps2=str(m)

# set first term and sum by hand
term[0]=str(x/N); term[1]=str(x % N)
Sum[0]=term[0]; Sum[1]=term[1]
num=1
den=2
while True:
# eps1 and eps2 are integers and should be fractions, hence term is shifted
    term=[Zero] + term[0:L-1]
    term=longmult(term, eps1)
    term=[Zero] + term[0:L-1]
    term=longmult(term, eps2)
    term=longmult(term, str(num))
    term=longdiv(term, str(den))
    Sum=longadd(Sum, term)
    num=num+2
    den=den+2
# test if term is zero, a rather crude method
    exit=True
    for t in term:
        if t!=Zero: exit=False
    if exit: break

print str(int(Sum[0])), '.'
for i in range(0,4):
    for j in range(0,5):
        for k in range(0,5): print Sum[25*i+5*j+k+1], ' ',
        print '\n',
    print '\n',
