forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_prime.py
40 lines (33 loc) · 983 Bytes
/
check_prime.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
37
38
39
40
# Author: Tan Duc Mai
# Email: [email protected]
# Description: Three different functions to check whether a given number is a prime.
# Return True if it is a prime, False otherwise.
# Those three functions, from a to c, decreases in efficiency
# (takes longer time).
from math import sqrt
def is_prime_a(n):
if n < 2:
return False
sqrt_n = int(sqrt(n))
for i in range(2, sqrt_n + 1):
if n % i == 0:
return False
return True
def is_prime_b(n):
if n > 1:
if n == 2:
return True
else:
for i in range(2, n):
if n % i == 0:
return False
return True
return False
def is_prime_c(n):
divisible = 0
for i in range(1, n + 1):
if n % i == 0:
divisible += 1
if divisible == 2:
return True
return False