chore(Python) : add prime number check (#802)

* Added Prime Number checker alorithm

* Added Prime Number Checker Algorithm:

* Update prime_number.py
pull/807/head
Hridyansh Pareek 2022-08-18 18:53:20 +05:30 committed by GitHub
parent 6e39b2bc60
commit 1d1a3468e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -83,3 +83,7 @@
## Queues
- [First in First out Queue](queues/fifo-queue.py)
##Number Theory
- [Prime Number Checker][number_theory/prime_number.py]

View File

@ -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.