Skip to content

Commit 22c8f5f

Browse files
solves sqrt in python
1 parent a6f4a95 commit 22c8f5f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![problems-solved](https://img.shields.io/badge/Problems%20Solved-107/571-1f425f.svg)
44
![problems-solved-java](https://img.shields.io/badge/Java-107/1571-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-20/1571-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-26/1571-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77

88
## Problems
@@ -24,7 +24,7 @@
2424
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LengthOfLastWord.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/length_of_last_word.py) |
2525
| 66 | [Plus One](https://leetcode.com/problems/plus-one) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PlusOne.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/plus_one.py) |
2626
| 67 | [Add Binary](https://leetcode.com/problems/add-binary) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/AddBinary.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/add_binary.py) |
27-
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/Sqrtx.java) |
27+
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/Sqrtx.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/sqrt.py) |
2828
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ClimbingStairs.java) |
2929
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RemoveDuplicatesFromSortedList.java) |
3030
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MergeSortedArray.java) |

python/sqrt.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def mySqrt(x: int) -> int:
2+
left, right, ans = 0, x, 0
3+
while left <= right:
4+
middle = left + (right - left) // 2
5+
result = middle ** 2
6+
if result == x:
7+
return middle
8+
elif result > x:
9+
right = middle - 1
10+
else:
11+
ans = middle
12+
left = middle + 1
13+
return ans
14+
15+
16+
# faster method, herons formula approach
17+
def heron_approximation(x: int) -> int:
18+
if x is 0 or x is 1:
19+
return x
20+
guess = x / 2
21+
epsilon = 10 ** -4
22+
while True:
23+
new_guess = 0.5 * (guess + x / guess)
24+
if abs(guess - new_guess) < epsilon:
25+
return int(guess)
26+
guess = new_guess
27+
28+
29+
# print(mySqrt(int(8)))
30+
31+
for i in range(50):
32+
print(f'sqrt({i})={mySqrt(i)}')

0 commit comments

Comments
 (0)