From 2d229759c3adc6f76d09a00f71e197967ff3b850 Mon Sep 17 00:00:00 2001 From: swamini-jadhav Date: Mon, 20 Oct 2025 16:09:40 +0530 Subject: [PATCH 1/4] Add Next_Prime_Number with prime checking logic Implement function to find the next prime number. --- maths/Next_Prime_Number.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 maths/Next_Prime_Number.py diff --git a/maths/Next_Prime_Number.py b/maths/Next_Prime_Number.py new file mode 100644 index 000000000000..03c0fc201e82 --- /dev/null +++ b/maths/Next_Prime_Number.py @@ -0,0 +1,24 @@ +""" +In the ancient city of Numeria, legends speak of an oracle whose whispers could bend the very fabric of mathematics. +Travelers from distant lands would bring her a number, and in return, she would reveal its future: the very next prime number. +Your task is to embody the oracle's wisdom. You will be given a number. +You must find the smallest prime number that is strictly greater than it. +""" +def check_prime(n): + if n<=1: + return False + if n<=3: + return True + if n%2==0 or n%3==0: + return False + temp=5 + while temp*temp<=n: + if n%temp==0 or n%(temp+2)==0: + return False + temp+=6 + return True +n=int(input()) +next_prime=n+1 +while not check_prime(next_prime): + next_prime+=1 +print(next_prime) From 443836e34f75738ead31222ad4bbf981b38404e0 Mon Sep 17 00:00:00 2001 From: swamini-jadhav Date: Mon, 20 Oct 2025 16:26:45 +0530 Subject: [PATCH 2/4] Rename Next_Prime_Number.py to next_prime_number.py --- maths/Next_Prime_Number.py | 24 ------------------------ maths/next_prime_number.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 24 deletions(-) delete mode 100644 maths/Next_Prime_Number.py create mode 100644 maths/next_prime_number.py diff --git a/maths/Next_Prime_Number.py b/maths/Next_Prime_Number.py deleted file mode 100644 index 03c0fc201e82..000000000000 --- a/maths/Next_Prime_Number.py +++ /dev/null @@ -1,24 +0,0 @@ -""" -In the ancient city of Numeria, legends speak of an oracle whose whispers could bend the very fabric of mathematics. -Travelers from distant lands would bring her a number, and in return, she would reveal its future: the very next prime number. -Your task is to embody the oracle's wisdom. You will be given a number. -You must find the smallest prime number that is strictly greater than it. -""" -def check_prime(n): - if n<=1: - return False - if n<=3: - return True - if n%2==0 or n%3==0: - return False - temp=5 - while temp*temp<=n: - if n%temp==0 or n%(temp+2)==0: - return False - temp+=6 - return True -n=int(input()) -next_prime=n+1 -while not check_prime(next_prime): - next_prime+=1 -print(next_prime) diff --git a/maths/next_prime_number.py b/maths/next_prime_number.py new file mode 100644 index 000000000000..1f7bb9546a34 --- /dev/null +++ b/maths/next_prime_number.py @@ -0,0 +1,30 @@ +""" +In the ancient city of Numeria, +legends speak of an oracle whose whispers +could bend the very fabric of mathematics. +Travelers from distant lands would bring +her a number, and in return, she would reveal +its future: the very next prime number. +Your task is to embody the oracle's wisdom. +You will be given a number. +You must find the smallest prime number +that is strictly greater than it. +""" +def check_prime(n): + if n<=1: + return False + if n<=3: + return True + if n%2==0 or n%3==0: + return False + temp=5 + while temp*temp<=n: + if n%temp==0 or n%(temp+2)==0: + return False + temp+=6 + return True +n=int(input()) +next_prime=n+1 +while not check_prime(next_prime): + next_prime+=1 +print(next_prime) From fef9291959506ae60ee371223b588ce0092ca567 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Oct 2025 10:59:26 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/next_prime_number.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/maths/next_prime_number.py b/maths/next_prime_number.py index 1f7bb9546a34..3055df010e25 100644 --- a/maths/next_prime_number.py +++ b/maths/next_prime_number.py @@ -1,30 +1,34 @@ """ -In the ancient city of Numeria, -legends speak of an oracle whose whispers -could bend the very fabric of mathematics. -Travelers from distant lands would bring -her a number, and in return, she would reveal +In the ancient city of Numeria, +legends speak of an oracle whose whispers +could bend the very fabric of mathematics. +Travelers from distant lands would bring +her a number, and in return, she would reveal its future: the very next prime number. -Your task is to embody the oracle's wisdom. +Your task is to embody the oracle's wisdom. You will be given a number. -You must find the smallest prime number +You must find the smallest prime number that is strictly greater than it. """ + + def check_prime(n): - if n<=1: + if n <= 1: return False - if n<=3: + if n <= 3: return True - if n%2==0 or n%3==0: + if n % 2 == 0 or n % 3 == 0: return False - temp=5 - while temp*temp<=n: - if n%temp==0 or n%(temp+2)==0: + temp = 5 + while temp * temp <= n: + if n % temp == 0 or n % (temp + 2) == 0: return False - temp+=6 + temp += 6 return True -n=int(input()) -next_prime=n+1 + + +n = int(input()) +next_prime = n + 1 while not check_prime(next_prime): - next_prime+=1 + next_prime += 1 print(next_prime) From 0dd9d9b92f7e9ad4ad20fed0ed1660ab98a3f2ef Mon Sep 17 00:00:00 2001 From: swamini-jadhav Date: Mon, 20 Oct 2025 17:14:12 +0530 Subject: [PATCH 4/4] Refactor prime number functions and add type hints --- maths/next_prime_number.py | 78 ++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/maths/next_prime_number.py b/maths/next_prime_number.py index 3055df010e25..00d15839eca8 100644 --- a/maths/next_prime_number.py +++ b/maths/next_prime_number.py @@ -1,10 +1,12 @@ """ +Next Prime Number -- https://en.wikipedia.org/wiki/Prime_number + In the ancient city of Numeria, legends speak of an oracle whose whispers could bend the very fabric of mathematics. -Travelers from distant lands would bring -her a number, and in return, she would reveal -its future: the very next prime number. +Travelers from distant lands would bring her a number, +and in return, she would reveal its future: +the very next prime number. Your task is to embody the oracle's wisdom. You will be given a number. You must find the smallest prime number @@ -12,23 +14,67 @@ """ -def check_prime(n): - if n <= 1: +def is_prime(number: int) -> bool: + """ + Check if a number is prime. + + >>> is_prime(2) + True + >>> is_prime(15) + False + >>> is_prime(19) + True + >>> is_prime(1) + False + >>> is_prime(-7) + False + """ + if number <= 1: return False - if n <= 3: + if number <= 3: return True - if n % 2 == 0 or n % 3 == 0: + if number % 2 == 0 or number % 3 == 0: return False - temp = 5 - while temp * temp <= n: - if n % temp == 0 or n % (temp + 2) == 0: + + factor = 5 + while factor * factor <= number: + if number % factor == 0 or number % (factor + 2) == 0: return False - temp += 6 + factor += 6 return True -n = int(input()) -next_prime = n + 1 -while not check_prime(next_prime): - next_prime += 1 -print(next_prime) +def next_prime(number: int) -> int: + """ + Find the smallest prime number strictly greater than the given number. + + >>> next_prime(2) + 3 + >>> next_prime(7) + 11 + >>> next_prime(14) + 17 + >>> next_prime(0) + 2 + >>> next_prime(-10) + 2 + """ + if not isinstance(number, int): + raise ValueError("next_prime() only accepts integral values") + + candidate = number + 1 + while not is_prime(candidate): + candidate += 1 + return candidate + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + try: + n = int(input("Enter an integer: ").strip() or 0) + print(f"The next prime number after {n} is {next_prime(n)}") + except ValueError: + print("Please enter a valid integer.")