-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicThreader.py
More file actions
31 lines (26 loc) · 800 Bytes
/
Copy pathBasicThreader.py
File metadata and controls
31 lines (26 loc) · 800 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
import math, threading
print_lock = threading.Lock()
prime_lock = threading.Lock()
prime = True # until proven otherwise
possibleprime = int(input("Prime Check\n>> "))
if str(possibleprime)[-1] in [1,3,7,9]:
def primecheck(lo, hi):
i = lo
while i < hi and prime:
if possibleprime % i == 0:
with prime_lock:
prime = False
# makes workers
for i in range(2, int(math.ceil(possibleprime/2.0))): # how many workers available
t = threading.Thread(target=primecheck) # put workers to work
t.daemon = True
t.start()
else:
with prime_lock:
prime = False
if prime:
with print_lock:
print("Prime")
else:
with print_lock:
print("Not Prime")