From 09ec4721baf169b3fb560b9e177bd0d6a993eaa5 Mon Sep 17 00:00:00 2001 From: WORLDSAVER Date: Mon, 15 Aug 2022 21:41:37 +0530 Subject: [PATCH] Added Prime Number Checker Algorithm: --- .../Python/number_theory/prime_number.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/algorithms/Python/number_theory/prime_number.py b/algorithms/Python/number_theory/prime_number.py index 54190ab1..063f16f8 100644 --- a/algorithms/Python/number_theory/prime_number.py +++ b/algorithms/Python/number_theory/prime_number.py @@ -1,16 +1,20 @@ #TO CHECK WHETHER A NUMBER IS PRIME OR NOT -N = int(input()) +def isPrime(N): -PRIME = True + for i in range(2, int(N**0.5) + 1): + if N%i==0: + return False -for i in range(2, int(N**0.5+1)): - if N%i==0: - PRIME = False - break + return True -if PRIME: - print(f"{N} is prime") -else: - print(f"{N} is not prime") \ No newline at end of file +# TIME COMPLEXITY - O(sqrt(N)) + +# EXAMPLES +# print(isPrime(3)) -> True +# print(isPrime(15)) -> False + +# We are just checking till sqrt(N) as if their is any factor of a number +# greater than sqrt(N) then it's partner will be less than sqrt(N) as if a*b>N +# and a>=sqrt(N) then b<=sqrt(N) as if b>sqrt(N) then a*b>N.