Skip to content

Commit 1b654b2

Browse files
authoredMar 20, 2018
Create Time Complexity: Primality
A prime is a natural number greater than that has no positive divisors other than and itself. Given integers, determine the primality of each integer and print whether it is Prime or Not prime on a new line. Note: If possible, try to come up with an primality algorithm, or see what sort of optimizations you can come up with for an algorithm. Be sure to check out the Editorial after submitting your code! Input Format The first line contains an integer, , denoting the number of integers to check for primality. Each of the subsequent lines contains an integer, , you must test for primality. Constraints Output Format For each integer, print whether is Prime or Not prime on a new line. Sample Input 3 12 5 7 Sample Output Not prime Prime Prime Explanation We check the following integers for primality: is divisible by numbers other than and itself (i.e.: , , ), so we print Not prime on a new line. is only divisible and itself, so we print Prime on a new line. is only divisible and itself, so we print Prime on a new line.
1 parent 3fb3d2a commit 1b654b2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎Time Complexity: Primality

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def checkPrime(p):
2+
if p < 2:
3+
return False
4+
elif p <= 3:
5+
return True
6+
elif p % 2 == 0 or p % 3 == 0:
7+
return False
8+
else:
9+
for i in range(3, int((p)**0.5+1), 2):
10+
if p % i == 0:
11+
return False
12+
return True
13+
14+
p = int(raw_input().strip())
15+
for _ in xrange(p):
16+
n = int(raw_input().strip())
17+
if checkPrime(n):
18+
print "Prime"
19+
else:
20+
print "Not prime"

0 commit comments

Comments
 (0)