Skip to content

Commit b76fec1

Browse files
committed
implemented square root with precision
1 parent 95c10f6 commit b76fec1

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

math/sqrt_precision_factor.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
Example:
77
Given N = 5 and P = 0.001, can produce output O such that
88
2.235 < O > 2.237. Actual square root of 5 being 2.236.
9-
10-
public static double squareRoot(int N, float P) {
11-
double guess = N / 2;
12-
13-
while( Math.abs( guess*guess - N ) > P) {
14-
guess = ( guess + ( N / guess ) ) / 2;
15-
}
16-
return guess;
17-
}
189
"""
1910

11+
def square_root(n,p):
12+
guess = float(n) / 2
13+
14+
while abs(guess * guess - n) > p:
15+
guess = (guess + (n / guess)) / 2
2016

17+
return guess

0 commit comments

Comments
 (0)