我正在迭代地搜索最接近大整数的幂和的列表。
我是多线程新手的一些背景。我的理解是,我当前的瓶颈可能可以通过多线程来缓解。但是,我不确定其他方法是否更适合进一步改进。
from itertools import combinations
from math import log, ceil
num = 26115155829
pwr = {n**p:str(n)+'^'+str(p) for n in range(9999,1,-1) for p in range(2,ceil(log(num,n)))}
pk = tuple(sorted(pwr.keys()))
sum2 = [(abs(num-(a+b)),' + '.join((pwr[a],pwr[b]))) for a,b in combinations(pk,2) if abs(num-(a+b)) < 99999]
sum3 = [(abs(num-(a+b+c)),' + '.join((pwr[a],pwr[b],pwr[c]))) for a,b,c in combinations(pk,3) if abs(num-(a+b+c)) < 99999]
Run Code Online (Sandbox Code Playgroud)
_ 描述 _
num:大整数的示例(通常为 10 到 18 位长) pwr:可能的基数^指数幂对的字典理解;4 到 1 位 基数开始递减,以便用较小的基数表示覆盖重复项,功率限制为低于目标大整数 sumN:到目标距离小于~100000的N次方总和的过滤列表理解
样本输出
>>> print(*sorted(sum2)[:100],sep='\n')
Run Code Online (Sandbox Code Playgroud)(57, '4774^2 + 2966^3') (1052, …
python performance multithreading combinations python-itertools