Last active
November 23, 2021 15:50
-
-
Save pathologicalhandwaving/67b956746c658b0972d7 to your computer and use it in GitHub Desktop.
sqRtAprox.py.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 = 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One thing, youre asking for input of an integer. You can put..
int(raw_input("")
In python 3.X the calculation of integers automatically changes into a float. The only reason why you'd use a float if youre requesting decimal/fractional input. Other than that, this code looks great!