Miller Rabin 素性测试准确性

2023-11-23

我知道米勒-拉宾素性检验是概率性的。不过我想用它来编程任务没有任何出错的余地。

如果输入数字是 64 位整数(即,long long in C)?


Miller–Rabin is indeed probabilistic, but you can trade accuracy for computation time arbitrarily. If the number you test is prime, it will always give the correct answer. The problematic case is when a number is composite, but is reported to be prime. We can bound the probability of this error using the formula on Wikipedia: If you select k different bases randomly and test them, the error probability is less than 4-k. So even with k = 9, you only get a 3 in a million chance of being wrong. And with k = 40 or so it becomes ridiculously unlikely.

That said, there is a deterministic version of Miller–Rabin, relying on the correctness of the generalized Riemann hypothesis. For the range u up to 264, it is enough to check a = 2, 3, 5, 7, 11, 13, 17, 19, 23. I have a C++ implementation online which was field-tested in lots of programming contests. Here's an instantiation of the template for unsigned 64-bit ints:

bool isprime(uint64_t n) { //determines if n is a prime number
    const int pn = 9, p[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 };
    for (int i = 0; i < pn; ++i)
        if (n % p[i] == 0) return n == p[i];
    if (n < p[pn - 1]) return 0;
    uint64_t s = 0, t = n - 1;
    while (~t & 1)
        t >>= 1, ++s;
    for (int i = 0; i < pn; ++i) {
        uint64_t pt = PowerMod(p[i], t, n);
        if (pt == 1) continue;
        bool ok = 0;
        for (int j = 0; j < s && !ok; ++j) {
            if (pt == n - 1) ok = 1;
            pt = MultiplyMod(pt, pt, n);
        }
        if (!ok) return 0;
    }
    return 1;
}

PowerMod and MultiplyMod只是在给定模数下使用 square-and-{multiply,add} 进行乘法和求幂的原语。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Miller Rabin 素性测试准确性 的相关文章

随机推荐

  • 通过采样/插值减小大数据集的大小以提高图表性能

    我有一大组 gt 2000 时间序列数据 我想在浏览器中使用 d3 显示它们 D3 非常适合向用户显示数据的子集 100 点 但我也想要一个 上下文 视图 像这样 显示整个数据集并允许用户选择子区域来详细查看 然而 当尝试在 d3 中显示这
  • Angular 5 - 将组件的名称实例化为字符串

    我知道如何使用初始化组件ComponentFactoryResolver resolveComponentFactory AComponentType 但该方法期望Type 而在我的代码中 我将类型的名称作为string 在 Angular
  • PHP 中的双向加密

    我的应用程序 显然 使用唯一的 ID 来区分记录 此 UID 在 URL 中传递 例如 examplepage php UID example int 除其他事项外 虽然我显然已经设置了服务器端验证来确保客户端不会访问其他客户端的数据 但我
  • Hibernate 与 Bean Validation API 结合使用时不遵循 JPA 规范?

    这个问题是这个问题的后续问题 JPA ConstraintViolation 与回滚 我做了一些关于 JPA 和验证 API JSR 303 组合的测试 我在中找到了以下内容JPA规格 第 101 102 页 默认情况下 默认的 Bean
  • 为什么 pandas isnull() 有效但 ==None 不起作用?

    我正在尝试选择的行df列所在的位置label有价值None 这是价值None我是从另一个函数获得的 而不是NaN 为什么df df label isnull 返回我想要的行 but df df label None 回报Empty Data
  • java中如何从线程返回值?

    在android中 我正在为url连接创建线程 在线程内部 我将响应消息存储在全局声明的字符串中 当我访问方法方法时 它返回null public class Rate fetch String total public String ra
  • 如何在 javaFX 中混合两个图像

    我有两个关于存储在两个单独图像中的数据的图 我需要将它们放在一张图像中 这样我才能看到差异 如何在javaFX中实现这一点 Solution 将两幅图像放在一个Group并应用一个混合模式通过设置最顶层Node的blendMode Imag
  • web 服务的 android.os.NetworkOnMainThreadException (ksoap)

    我是 android 编程新手 并尝试在此示例程序中使用 webservice 我使用 Android 4 1 我的 IDE 是 Eclipse Juno 我认为编程部分没问题 但可能连接有问题 package com example we
  • 在基元列表上使用 DataContractSerializer 自定义元素名称

    我对在 DataContractSerializer 中使用基元列表时设置自定义元素名称的最佳方法感兴趣 假设我有以下类 其中包含字符串列表作为数据成员 DataContract public class ClassName DataMem
  • Flex 中的 StringBuilder

    我正在寻找 Flex 中的快速字符串连接类 就像Java中的StringBuilder一样 Thanks var str1 String Vinoth var str2 String Babu var str3 String Chennai
  • 有没有办法从一系列数字中生成种子?

    例如 如果 java 生成伪随机序列 9 3 2 5 6通过使用23作为种子 我该如何做相反的事情 即得到23不按顺序9 3 2 5 6 或者如何为特定序列分配种子 如果有数据库 这很容易做到 只需为序列分配一个随机密钥 INSERT IN
  • SSIS 错误:源的外部列与数据源列不同步;如何删除外部列?

    查询应输出特定的项目列表 以及商店信息和经理信息等信息 使用光标翻阅各种不同管理级别的列表 选择相关信息 然后通过电子邮件向该人员发送查询为其地区 地区 商店返回的内容 我的问题是旅程中的 SSIS 阶段 尽管代码的行为就像它在运行一样 但
  • 如何在 VBA for Excel 中为动态选择的单元格定义 ENTER 按键事件

    I got a dynamically chosen Cell that will be filled with some information that im going to put and when I put the inform
  • 创建自定义简单光标适配器

    我想创建一个非常简单的光标自定义光标适配器 以方便在单击时更改行项目的颜色 使用以下代码 private static int save 1 public void onListItemClick ListView parent View
  • 创建多个轻量级 Google Cloud Functions 的最佳实践?

    Google Cloud Functions 的工作方式似乎是 你的模块进入一个functions目录 that functions目录然后包含一个package json文件包含所有模块之间的共享依赖项 每个模块可以包含许多导出函数 go
  • HTTP 错误 404.4 - 未找到您正在查找的资源没有与其关联的处理程序

    我在 IIS 中托管了一个网站 但每当我浏览该网站时 我都会收到 404 4 我该如何解决这个问题 我已经提到了几篇文章 他们都说问题与静态文件有关 但它已经被映射了 我还能做什么 这是我的 iis 7 0 中处理程序映射的附图 有任何想法
  • 使用 JFileChooser 将文件类型附加到 Java 中的文件

    我正在尝试使用 JFileChooser 保存图像 我只希望用户能够将图像保存为 jpg 格式 但是 如果他们不输入 jpg 则不会将其保存为图像 是否可以以某种方式将 jpg 附加到文件末尾 File file chooser getSe
  • 如何在 Bootstrap 中仅在特定屏幕尺寸上显示某些内容?

    我希望能够仅在 html 中显示图像md屏幕 我正在考虑隐藏图像sm向下 并躲避lg and up 我怎样才能做到这一点 在 Bootstrap v4 中 您可以使用这些类d none d md block d lg none使内容仅在媒体
  • UML泛化与实现

    我对 UML 还很陌生 所以我对泛化和实现有一些疑问 我正在对电子微控制器的行为进行建模 并且需要从 UML 描述生成 C 代码 据我所知 一个class realizes接口 这意味着它可以提供接口的实现 A概括两个类之间可能存在关系 在
  • Miller Rabin 素性测试准确性

    我知道米勒 拉宾素性检验是概率性的 不过我想用它来编程任务没有任何出错的余地 如果输入数字是 64 位整数 即 long long in C Miller Rabin is indeed probabilistic but you can