质数(prime number)又称素数,一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。
for i in list1:
flag = False
for j in range(2, i):
if i % j == 0:
flag = True
break
if flag:
print(i, "不是素数!")
else:
print(i, "是素数!")
import threading
lock = threading.Lock()
list1 = []
for i in range(2, 101):
list1.append(i)
def prime_number():
global list1
while list1 != []:
#锁定 多个线程调用一个对象,将其加锁,让其中一个线程调用完毕,再让其他线程调用
lock.acquire()
num = list1[0]
del list1[0]
#非锁定
lock.release()
flag = 0
for j in range(2, num):
if num % j == 0:
flag = 1
break
if flag == 0:
print(num, '是素数。')
else:
print(num, '不是素数。')
list_threading = []
for i in range(5):
t = threading.Thread(target=prime_number)
list_threading.append(t)
for i in list_threading:
i.start()
for i in list_threading:
i.join()