From 660943bf54700ba5f03f44d7ceec5f238432bc8d Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 06:16:30 +0200 Subject: [PATCH] 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. --- maths/perfect_square.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/maths/perfect_square.py b/maths/perfect_square.py index 107e68528068..46f6686f6f71 100644 --- a/maths/perfect_square.py +++ b/maths/perfect_square.py @@ -17,8 +17,12 @@ def perfect_square(num: int) -> bool: True >>> perfect_square(10) False + >>> perfect_square(11) + False + >>> perfect_square((2**53 + 1) ** 2) + True """ - return math.sqrt(num) * math.sqrt(num) == num + return math.isqrt(num) ** 2 == num def perfect_square_binary_search(n: int) -> bool: