Skip to content

Commit 6443a09

Browse files
authored
Merge pull request #905 from Manasi2001/issue-904
Prime Numbers
2 parents 1eb540e + 526752d commit 6443a09

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Numbers/Prime_Numbers.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Aim: To check if the entered numbers are prime or not.
3+
4+
'''
5+
6+
# importing necessary library
7+
from math import sqrt
8+
9+
# getting the number of test cases as input
10+
T = int(input().strip())
11+
12+
# checking condition
13+
for t in range(0,T):
14+
# getting each number as input
15+
n = int(input().strip())
16+
p = 0
17+
if n == 1:
18+
# '1' is not a prime number
19+
print('Not prime')
20+
else:
21+
# loop to calculate all factors of the entered number
22+
for i in range(1,int(sqrt(n)+1)):
23+
if n%i == 0:
24+
p+=1
25+
# if the factors are more than 2, then the number is not a prime one
26+
if p>=2:
27+
print('Not prime')
28+
else:
29+
print('Prime')
30+
31+
'''
32+
33+
COMPLEXITY:
34+
35+
Time Complexity -> O(N^2)
36+
Space Complexity -> O(N)
37+
38+
Sample Input:
39+
3
40+
12
41+
7
42+
3
43+
Sample Output:
44+
Not prime
45+
Prime
46+
Prime
47+
48+
Explanation:
49+
7 and 3 have only two factors --> 1 and the number itself.
50+
12 has factors --> 1, 2, 3, 4, 6 and 12, which are more than 2.
51+
So, it's not a prime number.
52+
53+
'''

0 commit comments

Comments
 (0)