-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctci-big-o.py
50 lines (37 loc) · 871 Bytes
/
ctci-big-o.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
41
42
43
44
45
46
47
48
49
50
"""
Time Complexity: Primality
https://www.hackerrank.com/challenges/ctci-big-o
"""
import unittest
import math
def is_prime(n):
if n == 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
upper_bound = int(math.sqrt(n) + 1)
for i in range(3, upper_bound + 1, 2):
if n % i == 0:
return False
return True
if __name__ == '__main__':
p = int(input().strip())
for a0 in range(p):
n = int(input().strip())
print("Prime" if is_prime(n) else "Not prime")
class TestIsPrime(unittest.TestCase):
def test_is_prime(self):
self.assertEqual(
False,
is_prime(12)
)
self.assertEqual(
True,
is_prime(5)
)
self.assertEqual(
True,
is_prime(7)
)