Miller-rabin判素数

用Miller-rabin判素数之前,先要知道一个叫费马小定理的东西。

费马小定理:如果p是质数,那么任意和p互质的数的p-1次方对p取模都等于一。

即:任意gcd(a,p)==1,那么a^(p-1)≡1(mod p)

既然我们用费马小定理又得到了一个新的质数的性质,那么我们就可以用这个性质来判定素数。

为了判定p是不是质数,我们随机检验一些a检验a^(p-1)mod p是否为1

但是这样判定一个素数并不是百分百正确的,有一些数不是素数,但依据费马小定理还是会判定成素数。

例如:p=561=3*11*17,无论如何取a,都满足费马小定理的素数性质。

于是我们就需要利用二次探测的思想:
p是质数,则x^2≡1(mod p)仅有两个解,x1=1,x2=p-1(很显然,(p-1)^2=p^2-2p+1),我们这样计算a^(p-1):

  1.我们令p-1=2^s * t(因为p是质数,p-1一定是偶数,偶数一定可以表示成2^s * t)

  2.然后我们来分解2^s * t,设x0=a^t,xi=x(i-1)^2,最后我们可以得到xs=a^(p-1)(等比数列)

  3.如果|xi|!=1,且x(i+1)=1,那么p显然不是质数(因为该方程只有两个解,如果)。

上面介绍了算法原理,下面是算法流程:

  1.按照上面的方法计算a^(p-1)(如果p不是质数,那么此时有可能直接返回)

  2.检查a^(p-1)≡1(mod p)

  3.当a是2~p-1的随机数时,如果p返回是合数,那他就是一定合数,如果返回是质数,则有一半的机会是质数

时间: 2024-08-29 01:15:30

Miller-rabin判素数的相关文章

miller——rabin判断素数

我们首先看这样一个很简单的问题:判定正整数\(n\)是正整数 最简单的做法就是枚举\(1\)到\(n\)的所有数看是否有数是\(n\)的因数,时间复杂度\(O(n)\) 稍微优化一下发现只要枚举\(2\)到\(\sqrt{n}\)中的数就可以了 然后发现数据范围\(n\leq 10^{18}\),期望执行次数直接就死掉了QAQ 我们就要考虑新的方法了 首先引入两个定理 1.费马小定理 如果\(p\)是素数,且\(gcd(a,b)=1\),那么\(a^{p-1}\equiv 1(mod \ n)\

51nod 1106 质数检测(miller rabin 素数测试.)

1106 质数检测 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出N个正整数,检测每个数是否为质数.如果是,输出"Yes",否则输出"No". Input 第1行:一个数N,表示正整数的数量.(1 <= N <= 1000) 第2 - N + 1行:每行1个数(2 <= S[i] <= 10^9) Output 输出共N行,每行为 Yes 或 No. Input示例 5 2 3 4 5 6

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

Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num

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

GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.

HDU1164_Eddy&amp;#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) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

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

GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.

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

Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num

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) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime

51_1037最长循环节 (miller rabin算法 pollard rho算法 原根)

1037 最长的循环节 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数. 1/6= 0.1(6) 循环节长度为1 1/7= 0.(142857) 循环节长度为6 1/9= 0.(1)  循环节长度为1 Input 输入n(10 <= n <= 10^18) Output 输出<=n的数中倒数循环节长度最长的

HDU 3864 D_num Miller Rabin 质数判断+Pollard Rho大整数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给出一个数N(1<=N<10^18),如果N只有四个约数,就输出除1外的三个约数. 思路:大数的质因数分解只能用随机算法Miller Rabin和Pollard_rho,在测试多的情况下正确率是由保证的. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <c