Rabin-Miller素性测试算法
来源:互联网 发布:sja1000 接收数据 编辑:程序博客网 时间:2024/05/21 21:34
算法原理:
Th1 p为素数, 0<a<p 如果a^2 mod p = 1 则 a mod p =1或者 a mod p =-1即a mod p = p-1
证明:
a mod p =1或者 a mod p =-1
Th2 任何一个素数p都能用表示
根据费马小定理 对于素数p和1<a<p-1 有
所以
由此可以得到数列中
(1)所有值都为1 或者
(2)有的值不为1 但是其平方后模p为1 所以该值为 -1mod p =p-1
如果一个数不满足上诉条件,则这个数一定为合数;
如果一个数满足上诉条件,则它可能是素数,也可能是合数,但它是素数的概率(0.75)大于
它是合数的概率。为了降低误判(将合数判为合数),多次运行该算法,降低错误率
算法实现:
bool millerRabinPrimeTest(unsigned long p,int cnt){//对于素数p进行检验 ,检验cnt次unsigned long k=0;unsigned long q= p-1;unsigned long r=0;if(q!=1&&q%2==1)return false;for (;q%2!=1;k++)q/=2;for (int i=0;i<cnt;i++){r=rand()%(p-1)+1;//从[1,p-1]随机选取一个数if(qe2(r,q,p)==1)continue;else{for (unsigned long j=0;j<k;j++){if(qe2(r,pow(2,j)*q,p)==p-1)break;else if(j==k-1)return false;}}}return true;}unsigned long qe2(unsigned long x,unsigned long y,unsigned long n){//使用加法链实现的模幂函数 即 x^y mod n unsigned long s,t,u;s=1;t=x;u=y;while(u){if(u&1)s=(s*t)%n;u>>=1;t=(t*t)%n;}
0 0
- Rabin-Miller素性测试算法
- Miller-Rabin素性测试算法详解
- Fermat素性测试, Miller-Rabin素性测试
- Miller–Rabin 素性测试
- Miller-Rabin大数素性测试
- 大素数判断_fermat素性测试+Miller-Rabin素性测试
- miller-rabin素性判定
- Miller-Rabin素性检测
- nyist 468 Fibonacci数列(六)(Miller-Rabin算法 大数素性测试)
- 大数素性测试+大数质因数分解(miller-rabin,Pollard_rho算法)
- Nefu 120 梅森素数【miller-rabin素性测试】
- C++实现的Miller-Rabin素性测试程序
- 分享一个C#的Rabin-Miller素性检验算法
- 素数测试算法-Miller-Rabin算法
- 大素数测试的Miller-Rabin算法
- HDU2138 随机素数测试 Miller-Rabin算法
- Miller-Rabin概率素数测试算法
- 【bzoj3667】Rabin-Miller算法 素数测试
- 字符设备驱动--- 设备号 设备文件
- 购物的奥妙!!
- 【自考】数据库系统原理(三)——考后总结
- Android使用adb获得activity堆栈信息
- 17.PHP内核探索:解释器的执行过程
- Rabin-Miller素性测试算法
- 批处理--从1累加100的和
- ps-help.exe
- String two pointer swapping (合集)
- IOS网络编程之——socket。
- 线段树 区间合并
- apache 去除index.php
- java web中的编解码问题
- github,git 入门配置和基本使用