Skip to content

Instantly share code, notes, and snippets.

@YUX-IO YUX-IO/main.py
Created Mar 23, 2020

Embed
What would you like to do?
import datetime
import multiprocessing as mp
from numba import njit
@njit(fastmath=True, cache=True, nogil=True)
def sumDivs(n):
s = 0
for k in range(2, 1 + int(n ** 0.5)):
if n % k == 0:
s += (k + (n // k))
s += 1
return s
@njit(fastmath=True, cache=True, nogil=True)
def lenAmis(starter, upperLimit, num_cores):
l = 0
for n in range(starter + 1, upperLimit, num_cores):
t = sumDivs(n)
if n == sumDivs(t) and n != t:
l += 1
return l
if __name__ == '__main__':
upperLimit = int(input("请输入上限(正整数):"))
num_cores = int(mp.cpu_count())
print("本地计算机有: " + str(num_cores) + " 核心")
pool = mp.Pool(num_cores)
start_t = datetime.datetime.now()
results = [pool.apply_async(lenAmis, args=(starter, upperLimit, num_cores)) for starter in
range(num_cores)]
print(sum(result.get() for result in results))
end_t = datetime.datetime.now()
elapsed_sec = (end_t - start_t).total_seconds()
print("多进程计算 共消耗: " + "{:.2f}".format(elapsed_sec) + " 秒")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.