我正在尝试使用Python制作Prime数字计算器。我设法写了一个顺序版本,但我必须让它并行。
要求:
素数必须由函数重新计算 在某个地方被抬头看。
必须有2个线程参与计算
该计划应尽可能快
现在我的代码是:
import sys
import math
import cProfile
def is_prime(num):
for j in range(2,int(math.sqrt(num)+1)):
if (num % j) == 0:
return False
return True
def prime(nth):
"""Return the nth prime number.
>>> prime(3)
The 3th prime number is: 5
>>> prime(4)
The 4th prime number is: 7
>>> prime(1000)
The 1000th prime number is: 7919
"""
i = 0
num = 2
while i < nth:
if is_prime(num):
i += 1
if i == nth:
print('The ' + str(nth) + 'th prime number is: ' + str(num))
num += 1
if __name__ == "__main__":
import doctest
doctest.testmod()
cProfile.run('print(prime(1000))')
答案 0 :(得分:0)
这是我目前的答案,
import threading, doctest, cProfile, time, random
result = [2]
counter = 1
def get_int(num):
for i in range(3, num):
yield i
def is_prime(num):
for j in range(2,int(num)):
if (num % j) == 0:
return False
result.append(num)
return True
def prime_calculator(nth):
lock = threading.Lock()
global result, counter, integer
while counter < (nth):
if is_prime(next(integer)):
lock.acquire()
try:
counter += 1
finally:
lock.release()
time.sleep(random.random()/1000)
def prime(nth):
"""Returns the nth prime number
>>> prime(1)
2
>>> prime(2)
3
>>> prime(4)
7
>>> prime(1000)
7919
"""
global integer, counter, result
integer = get_int(99999999)
threads = [threading.Thread(daemon=True, target=prime_calculator, args=(nth,)) for i in range(2)]
[thread.start() for thread in threads]
[thread.join() for thread in threads]
counter = 1
return result[-1]
if __name__ == "__main__":
doctest.testmod()
cProfile.run('print(prime(1000))')
然而,它不是线程安全的,因为它使用了一个计数器,稍后会尝试不使用计数器。