Skip to content

Commit 660943b

Browse files
committed
fix: check perfect squares with math.isqrt instead of floats
math.sqrt(num) * math.sqrt(num) == num rounds: it holds for 11, 14, 17 and about half of all other non-squares below 200000, and fails for large squares such as (2**53 + 1) ** 2. math.isqrt is exact, and the result now agrees with perfect_square_binary_search() for every n below 300000.
1 parent 6e03a48 commit 660943b

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎maths/perfect_square.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ def perfect_square(num: int) -> bool:
1717
True
1818
>>> perfect_square(10)
1919
False
20+
>>> perfect_square(11)
21+
False
22+
>>> perfect_square((2**53 + 1) ** 2)
23+
True
2024
"""
21-
return math.sqrt(num) * math.sqrt(num) == num
25+
return math.isqrt(num) ** 2 == num
2226

2327

2428
def perfect_square_binary_search(n: int) -> bool:

0 commit comments

Comments
 (0)