pathologicalhandwaving
9/11/2014 - 8:17 AM

sqRtAprox.py

sqRtAprox.py.py

#	Kristi Short
#	Math 180 Numerical Analysis
#	Fall 2014
#	Square Root Approximation
#	Pythonista Version

import math

#	prompt user for radicand
n = float(raw_input("Please enter an integer value for the radicand: "))
toleranceVal = float(raw_input("Please enter a positive integer value for the power of the tolerance: "))
tolerance = math.pow(10, -toleranceVal)

def findRoot(n, tolerance):
		sqRt = math.sqrt(n)
		floor = math.floor(sqRt)
		ceiling = math.ceil(sqRt)
		mid = float((floor + ceiling)/2)
		prevMid = mid
		
		while math.fabs((prevMid - mid) >= tolerance):
			
			if mid**2 > n:
				ceiling = mid
			else: 
				floor = mid
			
		return mid
		
root = findRoot(n, tolerance)
print "The approximated root of " + str(n) + " is " + str(root)