From 5cfbc2e982b2f5e29d1189bfda8f3d3af90435a0 Mon Sep 17 00:00:00 2001 From: Aqib Javid Bhat Date: Wed, 25 Oct 2023 20:00:02 +0530 Subject: [PATCH 1/3] Add Integer Square Root Algorithm --- maths/integer_square_root.py | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 maths/integer_square_root.py diff --git a/maths/integer_square_root.py b/maths/integer_square_root.py new file mode 100644 index 000000000000..0d6408853015 --- /dev/null +++ b/maths/integer_square_root.py @@ -0,0 +1,68 @@ +""" +Integer Square Root Algorithm + +This module provides an efficient method to calculate the square root of a +non-negative integer 'num' rounded down to the nearest integer. It uses +a binary search approach to find the integer square root without using any +built-in exponent functions or operators. + +Note: + - This algorithm is designed for non-negative integers only. + - The result is rounded down to the nearest integer. + - The algorithm has a time complexity of O(log(x)). + - Original algorithm idea based on binary search. +""" + + +def integer_square_root(num: int) -> int: + """ + Returns the integer square root of a non-negative integer num. + + Args: + num: A non-negative integer. + + Returns: + The integer square root of num. + + Raises: + ValueError: If num is negative. + + >>> integer_square_root(0) + 0 + >>> integer_square_root(1) + 1 + >>> integer_square_root(4) + 2 + >>> integer_square_root(625) + 25 + >>> integer_square_root(9) + 3 + >>> integer_square_root(10) + 3 + """ + if num < 0: + raise ValueError("num must be non-negative") + + if num < 2: + return num + + left_bound, right_bound = 0, num // 2 + + while left_bound <= right_bound: + mid = left_bound + (right_bound - left_bound) // 2 + mid_squared = mid * mid + + if mid_squared == num: + return mid + elif mid_squared < num: + left_bound = mid + 1 + else: + right_bound = mid - 1 + + return right_bound + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 53a10dfdd862385a5ce0188002f7fd84e3f46cc0 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 11:51:45 +0100 Subject: [PATCH 2/3] Update integer_square_root.py --- maths/integer_square_root.py | 57 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/maths/integer_square_root.py b/maths/integer_square_root.py index 0d6408853015..a4008b9242f6 100644 --- a/maths/integer_square_root.py +++ b/maths/integer_square_root.py @@ -1,11 +1,8 @@ """ -Integer Square Root Algorithm - -This module provides an efficient method to calculate the square root of a -non-negative integer 'num' rounded down to the nearest integer. It uses -a binary search approach to find the integer square root without using any -built-in exponent functions or operators. - +Integer Square Root Algorithm -- An efficient method to calculate the square root of a +non-negative integer 'num' rounded down to the nearest integer. It uses a binary search +approach to find the integer square root without using any built-in exponent functions +or operators. https://en.wikipedia.org/wiki/Integer_square_root Note: - This algorithm is designed for non-negative integers only. - The result is rounded down to the nearest integer. @@ -17,44 +14,50 @@ def integer_square_root(num: int) -> int: """ Returns the integer square root of a non-negative integer num. - Args: num: A non-negative integer. - Returns: The integer square root of num. - Raises: - ValueError: If num is negative. - - >>> integer_square_root(0) - 0 - >>> integer_square_root(1) - 1 - >>> integer_square_root(4) - 2 + ValueError: If num is not an integer or is negative. + >>> [integer_square_root(i) for i in range(18)] + [0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4] >>> integer_square_root(625) 25 - >>> integer_square_root(9) - 3 - >>> integer_square_root(10) - 3 + >>> integer_square_root(2_147_483_647) + 46340 + >>> from math import sqrt + >>> all(integer_square_root(i) == int(sqrt(i)) for i in range(20)) + True + >>> integer_square_root(-1) + Traceback (most recent call last): + ... + ValueError: num must be non-negative integer + >>> integer_square_root(1.5) + Traceback (most recent call last): + ... + ValueError: num must be non-negative integer + >>> integer_square_root("0") + Traceback (most recent call last): + ... + ValueError: num must be non-negative integer """ - if num < 0: - raise ValueError("num must be non-negative") + if not isinstance(num, int) or num < 0: + raise ValueError("num must be non-negative integer") if num < 2: return num - left_bound, right_bound = 0, num // 2 + left_bound = 0 + right_bound = num // 2 while left_bound <= right_bound: mid = left_bound + (right_bound - left_bound) // 2 mid_squared = mid * mid - if mid_squared == num: return mid - elif mid_squared < num: + + if mid_squared < num: left_bound = mid + 1 else: right_bound = mid - 1 From e0dc27fb4acbc2f71cc5183e996c96ffaf73e6f4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 11:55:39 +0100 Subject: [PATCH 3/3] Update integer_square_root.py --- maths/integer_square_root.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/maths/integer_square_root.py b/maths/integer_square_root.py index a4008b9242f6..27e874a43c79 100644 --- a/maths/integer_square_root.py +++ b/maths/integer_square_root.py @@ -2,7 +2,9 @@ Integer Square Root Algorithm -- An efficient method to calculate the square root of a non-negative integer 'num' rounded down to the nearest integer. It uses a binary search approach to find the integer square root without using any built-in exponent functions -or operators. https://en.wikipedia.org/wiki/Integer_square_root +or operators. +* https://en.wikipedia.org/wiki/Integer_square_root +* https://docs.python.org/3/library/math.html#math.isqrt Note: - This algorithm is designed for non-negative integers only. - The result is rounded down to the nearest integer. @@ -26,8 +28,8 @@ def integer_square_root(num: int) -> int: 25 >>> integer_square_root(2_147_483_647) 46340 - >>> from math import sqrt - >>> all(integer_square_root(i) == int(sqrt(i)) for i in range(20)) + >>> from math import isqrt + >>> all(integer_square_root(i) == isqrt(i) for i in range(20)) True >>> integer_square_root(-1) Traceback (most recent call last):