From 1d1a3468e7af883fd4c9657cc4e8b8a02811285a Mon Sep 17 00:00:00 2001 From: Hridyansh Pareek Date: Thu, 18 Aug 2022 18:53:20 +0530 Subject: [PATCH] chore(Python) : add prime number check (#802) * Added Prime Number checker alorithm * Added Prime Number Checker Algorithm: * Update prime_number.py --- algorithms/Python/README.md | 4 ++++ .../Python/number_theory/prime_number.py | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 algorithms/Python/number_theory/prime_number.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index b2be029d..7cfd6fa9 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -83,3 +83,7 @@ ## Queues - [First in First out Queue](queues/fifo-queue.py) + + +##Number Theory +- [Prime Number Checker][number_theory/prime_number.py] \ No newline at end of file diff --git a/algorithms/Python/number_theory/prime_number.py b/algorithms/Python/number_theory/prime_number.py new file mode 100644 index 00000000..aca81cfe --- /dev/null +++ b/algorithms/Python/number_theory/prime_number.py @@ -0,0 +1,22 @@ +#TO CHECK WHETHER A NUMBER IS PRIME OR NOT + + +def isPrime(N): + if N<=1: + return False + + for i in range(2, int(N**0.5) + 1): + if N%i==0: + return False + + return True + +# 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.