依据黎曼假设的Miller-Riemann素数测试

STEM版,合并数学,物理,化学,科学,工程,机械。不包括生物、医学相关,和计算机相关内容。

版主: verdeliteTheMatrix

回复
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 279
帖子: 13710
注册时间: 2022年 7月 26日 00:35

#1 依据黎曼假设的Miller-Riemann素数测试

帖子 TheMatrix楼主 »

据说是最快的:

https://www.zhihu.com/question/51236075 ... 5674802272

代码: 全选

import math
#import sympy
#import random

def is_prime_miller_riemann(n):
    if n % 2 == 0:
        return False
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    for a in range(2, min(n - 2, int(2 * math.log(n)**2)) + 1):
        x = pow(a, d, n)
        for _ in range(s):
            y = pow(x, 2, n)
            if y == 1 and x != 1 and x != n - 1:
                # x是1的非平凡模n平方根,提前终止
                return False
            x = y
        if y != 1:
            # 费马测试失败,提前终止
            return False
    return True

result = is_prime_miller_riemann(10000002039473)  # True 
print(result)

FoxMe(令狐)
论坛精英
论坛精英
帖子互动: 157
帖子: 5588
注册时间: 2022年 7月 26日 16:46

#2 Re: 依据黎曼假设的Miller-Riemann素数测试

帖子 FoxMe(令狐) »

不是最快的(可能是确定性算法中最快的)。有其它随机算法更快,也不需要黎曼假设。
回复

回到 “STEM”