-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.prime_number.py
More file actions
45 lines (38 loc) · 915 Bytes
/
8.prime_number.py
File metadata and controls
45 lines (38 loc) · 915 Bytes
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
# Method 1: Simple iterative solution
n=int(input("enter the number: "))
flag=0
for i in range(2,n):
if n%i==0:
flag=1
break
if flag==1:
print("The {} is not a prime number".format(n))
else:
print("The {} is a prime number".format(n))
# Method 2: Basic Recursion technique
num=int(input("enter the number: "))
def checkPrime(num,iter=2):
if num == iter:
return True
if num%iter==0:
return False
if num<2:
return False
return checkPrime(num,iter+1)
if checkPrime(num)==True:
print("The {} is not a prime number".format(num))
else:
print("The {} is a prime number".format(num))
# Method 3: Optimization by n/2 iterations
flag = 0
if num<2:
flag = 1
else:
for i in range(2,(num//2)+1):
if num%i==0:
flag = 1
break
if flag == 1:
print("The {} is not a prime number".format(num))
else:
print("The {} is a prime number".format(num))