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

算法是基于费马小定理(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. python tkinter菜单

    python3中,Tkinter编写菜单界面案例 from tkinter import * top=Tk() top.wm_title("菜单") top.geometry(&q ...

  2. Codeforces Round #569 (Div. 2) B. Nick and Array

    链接: https://codeforces.com/contest/1180/problem/B 题意: Nick had received an awesome array of integers ...

  3. python中json对象转换出错解决方法

    今天在使用python中的json转换碰到一个问题: 接收一个post的json字符串: s={"username":"admin","passwor ...

  4. Educational Codeforces round 78 A、B

    链接:https://codeforces.com/contest/1278 A:Shuffle Hashing 题意:对于一个字符串p可以执行一个"hash"操作,首先将p内的元 ...

  5. mysql jdbcTemplate访问

    String sql = "select * from xxx_photo_info where user_id in (:userIds)"; userIds从dao传过来时必须 ...

  6. hadoop伪分布式平台搭建

    1. 安装jdk1.7 JAVA_HOME=/home/hadoop/app/jdk1.7.0 vi /etc/profile.d CLASSPATH=.:$JAVA_HOME/lib/dt.jar: ...

  7. Node.js 几个重启工程的工具

    pm2, forever, nodemon, supervisor 均可在 npm 查找相关资料和用法. 线上工程推荐 pm2 开发推荐 supervisor

  8. JedisCluster和springboot整合

    maven依赖 springboot整合jedisCluster相当简单,maven依赖如下: <dependency> <groupId>org.springframewor ...

  9. Linux安装配置redis 、启动redis、redis设置密码

    由于间隔时间较长.机器的环境不同等等原因,所以每次安装redis的时候总是不那么顺利,所以这次我要做个笔记 文章大部分内容源于https://blog.csdn.net/gisredevelopmen ...

  10. git创建公钥匙

    目的: 使用SSH公钥可以让你在你的电脑和码云通讯的时候使用安全连接(git的remote要使用SSH地址) 1.打开终端进入.ssh目录输入当下命令 cd ~/.ssh 如果.ssh文件夹不存在,执 ...