美文网首页
python快速因式分解,13位数字秒解

python快速因式分解,13位数字秒解

作者: 星雨_2d3d | 来源:发表于2019-12-20 14:31 被阅读0次
def factorization(n,m=2,a=[],count_1=0):
    for i in range(m,int(n**0.5)+1):
        if n%i==0 and i!=1:
            a.append(i)
            count_1=1
            break
    if count_1==0:
        a.append(n)
        return a
    return factorization(int(n/i),m=i,a=a)

//运行以下代码
import time
t1=time.perf_counter()
print('因子:',factorization(9999999999972))
t2=time.perf_counter()
print('耗时:',t2-t1)

//返回
因子: [2, 2, 3, 3, 17, 227, 281, 256163]
耗时: 0.0012077499995939434

注意,当上述函数的参数为质数时,会返回质数的本身

相关文章

网友评论

      本文标题:python快速因式分解,13位数字秒解

      本文链接:https://www.haomeiwen.com/subject/fsfinctx.html