-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_square_root_newtons_method.py
More file actions
33 lines (30 loc) · 929 Bytes
/
2_square_root_newtons_method.py
File metadata and controls
33 lines (30 loc) · 929 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
#method 1:
def square_root(number):
estimate = float(number/2)
while True:
newestimate = float(abs(((estimate + (number/estimate))/2)))
if newestimate == estimate:
return newestimate
estimate = newestimate
num = int(input('Enter the number:'))
print square_root(num),'is the square root of',num
#method 2:
# def improve_guess(guess, num):
# return float((guess + (num / guess)) / 2.0)
#
# def good_enough(guess, num):
# d = abs(guess * guess - num) #guess squared ~= num
# tolerance = 0.0000001 #floating point accuracy, can be changed.
# return (d < tolerance)
#
# def start_guess(guess, num):
# while(not good_enough(guess, num)):
# guess = improve_guess(guess, num)
# return guess
#
# def square_root(num):
# r = start_guess(1, num)
# return r
#
# num = int(input('Enter the number:'))
# print square_root(num),'is the square root of',num