-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare root.py
More file actions
executable file
·56 lines (35 loc) · 1.02 KB
/
Square root.py
File metadata and controls
executable file
·56 lines (35 loc) · 1.02 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
while(1):
try:
#Bisection method
x = float(input("Enter a number to find the square root : "))
epsilon = 0.001
low = 0
high = x
ans = (low + high)/2.0
numGuesses = 0
while abs(ans**2-x)>=epsilon:
#print("not final but near " + str(ans))
if abs(ans**2)<x:
low = ans
else:
high=ans
ans = (low+high)/2.0
numGuesses+=1
print("Number of guesse = "+ str(numGuesses))
print("Final square root is : " + str(ans)+'\n')
except:
print("Failed !")
'''x =float(input("Enter a number to find square root : "))
epsilon = 0.01
step = epsilon**2
numGuesses = 0
ans = 0.0
while (abs(ans**2 - x)) >= epsilon and ans <= x:
ans+=step
numGuesses+=1
print("Number of guesse : "+ str(numGuesses))
if abs(ans**2)-x>=epsilon:
print("Failed!!")
else:
print("Square root is "+ str(ans))
'''