-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess-and-check, approximation & bisection.py
More file actions
54 lines (46 loc) · 1.32 KB
/
Guess-and-check, approximation & bisection.py
File metadata and controls
54 lines (46 loc) · 1.32 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
"""
Guess-and-check : runs through a while loop & does a guess and check, trying different versions of answers until it reaches a point close enough to the answer.
Approximation :
Bisection :
EXAMPLE : Square root
"""
def main():
bisection_search()
#def guess_and_check():
"""
input : asks for an integer
output: gives the square root of input
"""
answer = 0
neg_flag = False
x = int(input("Enter an integer: "))
if x < 0 :
neg_flaf = True
while answer ** 2 < x :
answer += 1
if answer ** 2 == x :
print("Square root of", x, "is", answer)
else :
print(x, "is not a perfect square")
if neg_flag:
print("Just checking... Did you mean", -x,"?")
#def approx_square_root():
def bisection_search():
x = int(input("Enter an integer: "))
epsilon = 0.01
num_guesses = 0
low = 1
high = x
answer = (low + high) / 2
while abs(answer ** 2 - x ) >= epsilon:
print("low = ", low, " high = ", high, " answer = ", answer)
num_guesses += 1
if answer ** 2 < x:
low = answer
else :
high = answer
answer = (low + high) / 2
print("After", num_guesses, "guesses")
print(answer, "is a very close approximation to the root of", x)
if __name__ == '__main__':
main()