We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a9667c0 commit 37a6ea9Copy full SHA for 37a6ea9
sqr.py
@@ -17,4 +17,22 @@ def mySqrt(x):
17
print(mySqrt(8)) # Output -> 2
18
print(mySqrt(-1))# Output -> 0
19
print(mySqrt(0)) # Output -> 0
20
-print(mySqrt(999))# Output -> 31
+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