测试单个素数,出错概率比计算机本身出错的概率还要低

算法是基于费马小定理(format),二次探测定理(x*x % p == 1 ,若P为素数,则x的解只能是x = 1或者x = p - 1)加上迭代乘法判断的Miller算法共同构成的

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <iostream>
#include <string> using namespace std; int N; int witness(int a, int n)//随机生成的a,来检测n的素性
{
int ans = ;
int t = n - ;//这里需要注意,你如果没有改变乘方的次数的话,最后的判断就是(ans == a) ? 0 : 1;
// 并且还要另外开辟空间来存储开始的a,比较麻烦,所以就这样了;
int x;
while (t)
{
if (t & )
{
ans = (long long int)ans * a % n;
}
x = a;//从这里开始就是迭代乘法,验证二次验证定理
a = (long long int)a * a % n;//这里就相当于 x*x % m = 1
if (a == && x != && x != (n - ))
{
return ; // 这里需要注意,返回一的话就说明,追踪过程中,出现了不是素数的依据.
}
t >>= ;
}
return (ans == ) ? : ;
} int MillerRobin(int n, int s) // 一般s取50就可以避免所有的偶然性了.
{
if (n == )
{
return ;
}
if (n < || !(n & ))
{
return ;
}
int a;
for (int i = ; i < s; i++)
{
a = (long long int )rand() * (n - ) / RAND_MAX + ; //这样生成的随机数就是真正的随机数了
if (witness(a, n))
{
return ;
}
}
return ;
} int main()
{
while (scanf("%d", &N) != EOF)
{
if (N == )
{
break;
}
if (MillerRobin(N, ))
{
printf("%d is a prime!\n", N);
}
else
{
printf("%d is not a prime!\n", N);
}
}
return ;
}

参考:https://blog.csdn.net/aledavvv/article/details/8929416

Miller-Robin 素数测试法 模板的更多相关文章

  1. 【数论】Prime Time UVA - 10200 大素数 Miller Robin 模板

    题意:验证1~10000 的数 n^n+n+41 中素数的个数.每个询问给出a,b  求区间[a,b]中质数出现的比例,保留两位 题解:质数会爆到1e8 所以用miller robin , 另外一个优 ...

  2. Miller Robin大素数判定

    Miller Robin算法 当要判断的数过大,以至于根n的算法不可行时,可以采用这种方法来判定素数. 用于判断大于2的奇数(2和偶数需要手动判断),是概率意义上的判定,因此需要做多次来减少出错概率. ...

  3. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

  4. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  5. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

  6. POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  7. HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. hdu 5901 Count primes 素数计数模板

    转自:http://blog.csdn.net/chaiwenjun000/article/details/52589457 计从1到n的素数个数 两个模板 时间复杂度O(n^(3/4)) #incl ...

  9. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

随机推荐

  1. 【NOIP2016提高A组五校联考1】排队

    题目 分析 首先预处理出每个点的优先级,当有一个人进入时,一定会走到优先级最大的空房间中. 把所有空的房间扔到一个堆中,按优先级大小维护这个堆. 答案怎么求就不说了,很容易想到,就只讲操作吧. 对于第 ...

  2. 【leetcode】1219. Path with Maximum Gold

    题目如下: In a gold mine grid of size m * n, each cell in this mine has an integer representing the amou ...

  3. vue-router的路由

    路由和组件是有区别的:组件一般是在同一个页面的不同模块,但是路由是直接切换到另一个页面,之前的页面销毁. App.vue中的router-view会渲染顶级路由匹配到的组件.组件内部嵌套的router ...

  4. HTML5测试(二)

    HTML5测试(四) 1.input 元素中,下列哪个类型属性定义了输入电话号码的控件? A.mob B.tel C.mobile D.telephone 答案:B 具有 type 属性的 input ...

  5. 186. [USACO Oct08] 牧场旅行 (第三次考试大整理)

    186. [USACO Oct08] 牧场旅行 输入文件:pwalk.in   输出文件:pwalk.out   简单对比 时间限制:1 s   内存限制:128 MB n个被自然地编号为1..n奶牛 ...

  6. Shoi2017试题泛做

    一口气做完六个省的省选(误) Day1 [Shoi2017]期末考试 枚举最大的天数,然后代价贪心地O(1)计算. #include <cstdio> #include <algor ...

  7. [LeetCode]-011-Roman_to_Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. MongoDB通过JavaDriver执行shell命令,例如创建sharding collection

    Mongodb的java driver本身的接口 void createCollection(String collectionName, CreateCollectionOptions create ...

  9. 接入集团auth流程

    前言 一直对集团的auth系统很感兴趣,所以这次记录下接入集团auth的流程.如果后期有时间,会补充具体的auth实现细节. 正文 一.实现思想 1. 实现思想 明确几个名词:接入方,管理方.接入方指 ...

  10. nginx实现域名跳转

    server { listen 80; server_name www.dd.com www.tt.com; index index.html index.htm index.php; root /u ...