Skip to content

Commit 37a6ea9

Browse files
committed
added new solution
1 parent a9667c0 commit 37a6ea9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

sqr.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,22 @@ def mySqrt(x):
1717
print(mySqrt(8)) # Output -> 2
1818
print(mySqrt(-1))# Output -> 0
1919
print(mySqrt(0)) # Output -> 0
20-
print(mySqrt(999))# Output -> 31
20+
print(mySqrt(999))# Output -> 31
21+
22+
# Solution 2 from LinkedIn follower
23+
def mySqrt2(x):
24+
a, b = 0, x
25+
x0 = 0
26+
while abs(x0*x0 - x) > 0.001:
27+
x0 = (a + b) / 2
28+
if x0*x0 > x:
29+
b = x0
30+
else:
31+
a = x0
32+
return int(x0)
33+
34+
print(mySqrt2(8)) # Output -> 2
35+
print(mySqrt2(-1))# Output -> 0
36+
print(mySqrt2(0)) # Output -> 0
37+
print(mySqrt2(999))# Output -> 31
38+

0 commit comments

Comments
 (0)