From 62082e017d6a9dcb3c34d6d1d6b990dbeb99dc66 Mon Sep 17 00:00:00 2001 From: Rudra Patni Date: Sun, 5 Oct 2025 16:56:52 +0530 Subject: [PATCH 1/5] Add a prime check function --- maths/basic_maths.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 833f31c18b9e..1001afb0b8ad 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -116,6 +116,35 @@ def euler_phi(n: int) -> int: return int(s) +def isPrime(n: int) -> bool: + """Checks if a number is prime. + >>> isPrime(11) + True + >>> isPrime(15) + False + >>> isPrime(1) + False + Traceback (most recent call last): + ... + ValueError: Only integers greater than 1 can be prime + >>> isPrime(0) + Traceback (most recent call last): + ... + ValueError: Only integers greater than 1 can be prime + """ + if n<=1: + raise ValueError("Only integers greater than 1 can be prime") + if n==2 or n==3: + return True + if n%2==0 or n%3==0: + return False + for i in range(5, int(math.sqrt(n)) + 1, 6): + if n%i==0 or n%(i+2)==0: + return False + + return True + + if __name__ == "__main__": import doctest From 0a2bc4b4d411eab73e41ead1749dee85cd83988e Mon Sep 17 00:00:00 2001 From: Rudra Patni Date: Sun, 5 Oct 2025 17:19:02 +0530 Subject: [PATCH 2/5] Update the prime check function --- maths/basic_maths.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 1001afb0b8ad..cb61fdf38bb2 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -116,18 +116,21 @@ def euler_phi(n: int) -> int: return int(s) -def isPrime(n: int) -> bool: +def is_prime(n: int) -> bool: """Checks if a number is prime. - >>> isPrime(11) + + Source: https://en.wikipedia.org/wiki/Prime_number + + >>> is_prime(11) True - >>> isPrime(15) + >>> is_prime(15) False - >>> isPrime(1) + >>> is_prime(1) False Traceback (most recent call last): ... ValueError: Only integers greater than 1 can be prime - >>> isPrime(0) + >>> is_prime(0) Traceback (most recent call last): ... ValueError: Only integers greater than 1 can be prime From c2fe69e56c15976acc7891309115b4004f256bb8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 11:52:02 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/basic_maths.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index cb61fdf38bb2..d1cb1f5404b9 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -135,16 +135,16 @@ def is_prime(n: int) -> bool: ... ValueError: Only integers greater than 1 can be prime """ - if n<=1: + if n <= 1: raise ValueError("Only integers greater than 1 can be prime") - if n==2 or n==3: + if n == 2 or n == 3: return True - if n%2==0 or n%3==0: + if n % 2 == 0 or n % 3 == 0: return False for i in range(5, int(math.sqrt(n)) + 1, 6): - if n%i==0 or n%(i+2)==0: + if n % i == 0 or n % (i + 2) == 0: return False - + return True From c9b3b9393be83060b54e8f6476b05e46b79508d3 Mon Sep 17 00:00:00 2001 From: Rudra Patni Date: Sun, 5 Oct 2025 17:29:15 +0530 Subject: [PATCH 4/5] Updated the prime check function --- maths/basic_maths.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index cb61fdf38bb2..e08f30a44f83 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -137,7 +137,7 @@ def is_prime(n: int) -> bool: """ if n<=1: raise ValueError("Only integers greater than 1 can be prime") - if n==2 or n==3: + if n in {2, 3}: return True if n%2==0 or n%3==0: return False From bb71a757b7aaaa55ab325b7aab08293e3c231644 Mon Sep 17 00:00:00 2001 From: Rudra Patni Date: Mon, 6 Oct 2025 17:12:15 +0530 Subject: [PATCH 5/5] Update is_prime function --- maths/basic_maths.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 25c1065f9d0c..4f89f8607a2d 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -127,16 +127,13 @@ def is_prime(n: int) -> bool: False >>> is_prime(1) False - Traceback (most recent call last): - ... - ValueError: Only integers greater than 1 can be prime >>> is_prime(0) - Traceback (most recent call last): - ... - ValueError: Only integers greater than 1 can be prime + False + >>> is_prime(2) + True """ if n <= 1: - raise ValueError("Only integers greater than 1 can be prime") + return False if n in {2, 3}: return True if n % 2 == 0 or n % 3 == 0: