# home_dir is different for a web user and for a native user on a web server
# So, do the following check
if os.environ.has_key("DOCUMENT_ROOT"):
doc_root = os.environ["DOCUMENT_ROOT"]
home_dir = get what you need from doc_root
#home_dir = doc_root.split("something")[0]
elif os.environ.has_key("HOME"):
doc_root = os.environ["HOME"]
home_dir = doc_root+"/"
Tuesday, August 19, 2014
Python: getting the home directory for a user
Python: square root with one decimal value
square root with one decimal value
############################################################################
def find_close_int_sqrt_and_diff(x):
ans = 0
diff1 = 0
dec_ans = 0
decimal_part = 0.0
if x <= 0: return 0,0
# if x==1: return 1,0
# if x==2: return 1.414,0
else:
for i in range(1,x+1):
t = i*i
if t==x:
ans = i
diff1 = 0
break
elif t > x:
sqrt_int = i-1
ans = ans + sqrt_int
tmp_sqr = sqrt_int*sqrt_int
diff1 = x-tmp_sqr
break
return ans, diff1
############################################################################
def find_dec_part(x, n):
## x - the number seeking squreroot
## n the larget integer whose sqr is less than x
## Lets find the decimal part
## using the method http://mathforum.org/library/drmath/view/52608.html
dec_part = 0
k = x-n*n
l = 20*n
m = k*100
if l < m:
for i in range(11):
#print i*l, m
if i*l < m: continue
else:
dec_part = i-1
break
return dec_part
############################################################################
for k in range(90,101):
a, d = find_close_int_sqrt_and_diff(k)
dp = find_dec_part(k,a)
print(" closest sqrt value of %d is %d.%d " %(k,a,dp))
Sample results:
closest sqrt value of 11080 is 105.2
closest sqrt value of 11081 is 105.2
closest sqrt value of 11082 is 105.2
closest sqrt value of 11083 is 105.2
closest sqrt value of 11084 is 105.2
closest sqrt value of 11085 is 105.2
closest sqrt value of 11086 is 105.2
closest sqrt value of 11087 is 105.2
closest sqrt value of 11088 is 105.2
closest sqrt value of 11089 is 105.3
closest sqrt value of 11090 is 105.3
closest sqrt value of 11091 is 105.3
closest sqrt value of 11092 is 105.3
closest sqrt value of 11093 is 105.3
closest sqrt value of 11094 is 105.3
closest sqrt value of 11095 is 105.3
closest sqrt value of 11096 is 105.3
closest sqrt value of 11097 is 105.3
closest sqrt value of 11098 is 105.3
closest sqrt value of 11099 is 105.3
closest sqrt value of 11100 is 105.3
closest sqrt value of 11101 is 105.3
closest sqrt value of 11102 is 105.3
closest sqrt value of 11103 is 105.3
closest sqrt value of 11104 is 105.3
closest sqrt value of 11105 is 105.3
closest sqrt value of 11106 is 105.3
closest sqrt value of 11107 is 105.3
closest sqrt value of 11108 is 105.3
closest sqrt value of 11109 is 105.3
closest sqrt value of 11110 is 105.4
[Finished in 0.0s]
Friday, August 15, 2014
Python: Use of recursive method to find the square root from scratch
Use of recursive method to find the square root from scratch
######################
### Recursive has a problem of bringing the results a bit less
def find_sqrt_2(x):
ans = 0
diff1 = 0
dec_ans = 0
decimal_part = 0.0
if x <= 0: return 0
if x==1: return 1
if x==2: return 1.414
else:
for i in range(1,x+1):
t = i*i
if t==x:
ans = i
dec_ans = 0
break
elif t > x:
sqrt_int = i-1
ans = sqrt_int
tmp_sqr = sqrt_int*sqrt_int
diff1 = x-tmp_sqr
### Calculating the decimal part of the sqrt
if diff1 >0:
dec_ans = dec_ans + find_sqrt_2(diff1)
break
return ans+dec_ans/float(10)
Python script to calculate an approximate square root - from scratch
#!/usr/bin/python
def find_close_int_sqrt_and_diff(x):
ans = 0
diff1 = 0
if x <= 0: return 0,0
# if x==1: return 1,0
# if x==2: return 1.414,0
else:
for i in range(1,x+1):
t = i*i
if t==x:
ans = i
diff1 = 0
break
elif t > x:
sqrt_int = i-1
ans = ans + sqrt_int
tmp_sqr = sqrt_int*sqrt_int
diff1 = x-tmp_sqr
break
return ans, diff1
############################################################################
k=249
l2, d = find_close_int_sqrt_and_diff(k)
while (d>0):
i, d = find_close_int_sqrt_and_diff(d)
l2 = l2+i/float(10)
print("the closest sqrt value of %d is %3.2f and the sqr is %3.2f" %(k, l2, l2*l2))
Subscribe to:
Comments (Atom)