forked from tiyd-python-2015-01/approximate-square-root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare_root.py
More file actions
38 lines (28 loc) · 1008 Bytes
/
Copy pathsquare_root.py
File metadata and controls
38 lines (28 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#Newton's method of successive approximations says that whenever we have a
#guess y for the value of the square root of a number x, we can get a
#better guess (one closer to the actual square root) by averaging y with x/y. If we do this over and over, we should be able to get a very accurate guess.
#You have to write a script that asks the user for a positive number and
#then compute the square root with a maximum error of 0.1.
#You then print out your answer to the user.
# x = int(input ("Give me a positive number: "))
# y = 0.1
#
# while y < (x / 2):
# if (y ** 2) <= (x - .01):
# y += 0.1
# else:
# break
#
# print("The square root of {} is {}.".format(x, round(y, 3)))
# x = input ("Give me number: ")
# y = (int(x)**0.5)
# print (y)
num = int(input ("I need a positive number: "))
tolerance = 0.1
y = 0.1
while y < (num / 2):
if y**2 < (num - tolerance):
y += 0.1
else:
break
print("The square root of {} is {}.".format(num, round(y, 3)))