-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime.py
More file actions
52 lines (45 loc) · 1.5 KB
/
Copy pathprime.py
File metadata and controls
52 lines (45 loc) · 1.5 KB
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
51
52
from time import sleep
from math import sqrt
firstValue = input("First Value: ")
endValue = input("End Value: ")
try:
firstValue = int(firstValue)
endValue = int(endValue)
except ValueError:
print("Error: First Value and End Value must be filled a number")
else:
if firstValue >= 2 and firstValue < endValue:
count = 0
percentage = 0
counting = firstValue
print("Prime Numbers: ", end="")
while counting <= endValue:
a = counting // 2
b = sqrt(counting)
if counting <= 3:
print(counting, end=" ")
counting += 1
count += 1
elif counting % 2 == 0:
counting += 1
elif b == int(b):
counting += 2
elif a >= 2:
p = None
for i in range(3, a + 1, 2):
if counting % i == 0:
p = False
break
if p == None:
print(counting, end=" ")
count += 1
counting += 2
print()
percentage = (count / (endValue - firstValue + 1)) * 100
print(f"Total prime numbers found: {count}")
print(f"Percentage of prime numbers: {percentage:.2f}%")
print()
print("Done")
else:
print("Error: Sorry, the Program cannot be run because First Value is less than 2 or higher than End Value")
input("Press ENTER to exit")