Created
September 12, 2014 03:46
-
-
Save pathologicalhandwaving/c5e833463124f82aa929 to your computer and use it in GitHub Desktop.
sqRtAprox.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 #THIS IS THE PROBLEM | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment