forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime_check.py
36 lines (31 loc) · 853 Bytes
/
prime_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
prime_test(n) returns a True if n is a prime number else it returns False
"""
def prime_check(n):
if n <= 1:
return False
if n == 2 or n == 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
j = 5
while j * j <= n:
if n % j == 0 or n % (j + 2) == 0:
return False
j += 6
return True
def prime_check2(n):
# prime numbers are greater than 1
if n > 1:
# check for factors
for i in range(2, int(n ** 0.5) + 1):
if (n % i) == 0:
# print(num, "is not a prime number")
# print(i, "times", num//i, "is", num)
return False
# print(num, "is a prime number")
return True
# if input number is less than
# or equal to 1, it is not prime
else:
return False