判断正整数p是否是素数
方法一 朴素的判定 aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAADUAAAAUBAMAAADfOGjQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHZmmasi3Ym7RO8yVM0Ml5alAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABPUlEQVQoFV2RPUvDUBSGn9Q01iapQVycLBbBRawUN7G6OxQXHRyyiDjZxVHILBTq4lwQJxWK/QMO7opObvEfVIROBX2T3qbqITfvV+45SS78L6/+ndQfe86omSAl5fRuLdV2RBqpgKcRzqewEXAckOuY7GqEflVY0IZCm5KJvPH+WMZtAMU+bybLh4a0wOqJe0NMK45MxI3afUr4A7rG24XX9dohuDDdlmn3aY4yq41z1+MdZnVFMt2GE6uzlv2IP9VkWV7AYlnZWpj411+wIGVH7KlhgBtJPWAlPffhQuB2GKTz7BjymtmV+dzRODjFjzvJu1jncCDjQ6sU514EW9hRlROxQmUlFKxqFYfp36ngtQJqMkyl33yWPGHqckwgPZp6nBlOI6M42+JuNTPsIKNwL+5N9OaEgvNbQAg/B2w73MSNaYAAAAAASUVORK5CYII=" alt="">
方法二 线筛 aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAATBAMAAAAOpGKDAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHZmmasi3Ym7RO8yVM0Ml5alAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA9UlEQVQYGT2QMU7DQBBF38bGLIkNKy4AIkKiQQRFtCjcwKKioEiDKDEHQPIFkFJRuwckQy7AERBU6cwNTJOKgr92nJF2/vt/RrvSQle7LfQaMQfjC0HaZmbi9cxx6+gVnlXfOlYLdsZ2E6h5eHbQr5txE9tPzFwU//HUBGr9FPsrTZaU8HU6vpYp2ZxJwpqM6GXOQqZmJ5cM0qgiCTIOZd7Y25ecTONK2zmXMvcMcsk7JtN2wVKmJKxgQ3fqjTuSqvBkHuBK0x84J8xHRFqww6OpsmMYEj86gpFcWzcdbLmOWON6CNFkNfb/1tVrC8FHF0ijliX/HNUrtUeIY3MAAAAASUVORK5CYII=" alt="">
对于大整数(>1e9),可以优化
方法三 利用费马小定理
假如p是质数,且gcd(a,p)=1,那么 a^(p-1) ≡1(mod p)。
即:假如a是整数,p是质数,且a,p互质,那么a的(p-1)次方除以p的余数恒等于1。
我们这样写:a*a^(p-2) ≡1(mod p)
根据定义,a^(p-2)就是a (模p)的逆元
那么反过来,随机整数a能使p满足a^(p-1) ≡1(mod p)的话,那么p就是质数?
当然不是,虽然很大可能是,但是会有特殊的存在。
如2^340≡1(mod 341),但341=31*11
具体来说,特殊的数是费马伪素数和卡迈克尔数,这里不介绍了
方法四 二次探测定理
对于任意素数p, 满足方程aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMUAAAAVBAMAAAAEKDfsAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMARFSZImYQMrvddqvNie8Nf0xcAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC3UlEQVRIDbVUTUgUYRh+Znd+9sd2h6JDSTZYkAiVh5IoghEpIqodMGtFpKHoUBAOBREkbQhGeMkunjfCFL0sBdVFWCQIgij2kAcJ9ODdQLZLUe/3ffO5M+OsdfGD/d7ned5n3ne+n1mAjcRQF4/bOZ3H/HaW57VHUIzpoZdjRC41TTR7gOvfY7LZqGY8s4V0I5r5H65Phl3KR+IvwhqxNl9JOALsHTI3eZoKWS+UUqd/Eq+GNEbmpFLlwKjig1T+HafViId6yJcNZMYkrnGQsvBSKqorkYxnJRBRMTMMnHjKBpeoR8rmKDitS9LKQd7b2D3jjStzfnzY3RB2HtvdXfjzuyFwRD1ygDZ1c/B++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hwOobUafjWWLwl4N+bxwPL2azYqTjmY4ydfqmGTXhCYPdlkaLqezuaBi0wR0Y3IGtvQMvWdOlZmGRJX88ELIkNCU5ElOzcBTK4TTSoFTEN57eUrKwFn5VG7NhWVK6oWxl4dBkY2EpARMv6AoaE8HzluuujNBhRGNh7mOyMLfwnjyM9q6kkcvoLMzdKEnPI1VnLf8sTJ7aP8UjWN/Bt9Ct0/sCGshldBYmOzUqo7IwU0eXkYU3jrl3/A47UbWrhe0c2U4AvJJfHK6SrJTRWJjs1KiMysJMHV3KV6oHHacDyk7ItvEjL7+RY25KGY2FeR62moCPLH3lbVQWm/CpbMNPJqbE667WF9+FK2T0KLOwxMlUhrEw9tY2/tYCfv548ItsdfGmQdkLVcpqNBbmQYnP5kc4mYdK4FDi6Nl0AKozAfttUZ8N+xwyOTmxzAX4YAi/j8p1PjHWX8Qj7bngKSaL1eCGyGoszBzwIliYK/R+GSbxo/LJU6r+IzwxnV4NS3fLWFk4jLKysJqQjRZ1HcEob3w1NNZZmPtQbwVS2QHEZyrXYjrRXEmm8jHprCwskpCAh+Nay05piu4wLQsrWXZcV4Y02GZoSJGaha1ZJGVIvZKCielZWM8wGA/uwP/xDriNuFXHGuICeulLulE9UtWt6cavkfu/9I98aSbbUY5DyxXu1+ZC3z2VYZRkOZoV7yr1BHaTtTLseG8h9BZlCr4RLt5VPanTCBO0KVlEmYQjVSkkSRS0yR1xXd8sLS2wm5ZwpMpDNtEL2pR8xWr29j5SlUISRURQbCXrs0k4Uk3MLTlw2pRUYjV0NLs1/8gXtGkrg1tga/6Rz2nTsqERLROOVKWZJYgRbXLH7k4b/COf35DUfcKRaup8MQGOZxrkI1XT2p/GjwtDw9+JGuW4BQ3PCgAAAABJRU5ErkJggg==" alt="">
方法五 Miller Rabin算法
逆用费马小定理加上二次探测,就得到了 Miller Rabin 算法。
Miller Rabin 算法有一定的出错概率,出错的情况一定是将合数判定为素数,且出错概率极低
对于一个奇素数(偶素数只有2嘛),显然p-1是偶数
则有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nM9DJhpy+4ExA8lrsbtIJ3Ud82PEojG/WR4L5q7IXEUmGnL7gcwkvfAj1nbM+08/aJbHghM1mauoRMOwZxF9k5vBt/8KUh1dxzySD4hks/zv6A6Zq4SJxqA9C7r/J696/i/MTYaO+QUiiGSz/O3TjsxVjImG4caJ3zHsG6jKQVQPWQNEGjInGhplQMJL2dV2zFttvBWuzs6caOi0Ay9WA482SXkXOUsjiXPCCmLxkOT/fzmi7ZgXmcQRzUJpiYZGncijCnBLKxM/g3oR60P8P31FqZtufNi8AAAAAElFTkSuQmCC" alt="">
所以算法实现过程有两步:
1.枚举k,对于每一个k,检验是否满足二次探测定理(即上面的最后一步)
2.再检验aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAAQBAMAAACIMBAtAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAdlQyIrtmq82J70QQmd0a05NiAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAsklEQVQYGWNgQAZMmReQuWC2I0Mlw7ZcVGELhlSeAHewGI/kpitJExgOAjkzmQXSwGJs/AaiDA/YHZMYeN4wzG6B6DVlqOZ5wHNvAwO7AQNDA0RMkOEN0wV2zdsMwiwMTG8gYsU8D9i3MZxk4N7ACBZgFDJhCGCewCjMwMPg//8nWCyMoQKiGEGyBQDdiQa4EhhWowkx+Dswf0AXs2dgDNiAJsjFcGJCApoY9yXlawdQxQB8VCWO20gk9wAAAABJRU5ErkJggg==" alt="">即aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAQBAMAAACFLmBqAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAdlQyIrtmq82J70QQmd0a05NiAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAg0lEQVQYGWNgQAY8qsg8IJtJA02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Q8QtZkhT+AOtwZG8D7+pXAAAAAElFTkSuQmCC" alt="">
对于任意一个p,如果通过了算法检验,则它不是素数的概率是25%
那么k轮之后,p不是素数的概率就是0.25^k,就很小了
一般8-10轮就差不多了
那么对于a的选择,可以选前面几个质数:2,3,5,7,11等,也可以用随机数
时间复杂度一般是aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAAATBAMAAADBilZAAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHZmmasi3Ym7RO8yVM0Ml5alAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABkklEQVQoFV1Sv0vDYBB9aWKMbdoEVxGLIrio1VJxkvofBCcHhy6igz/BSRQCxUFQ6OTgYvYqRvsPOAo6lNbFRSLOQlwydfBdTSR68H3v3b2XuxwJkMRwQv5hpp8r4+VlEgfmlQ0YB/9MSlUKFRsbNjIe0JT0U650dJkYDk8DBdIT0b7kSocoTc7IhhD/B092L20QbrShtIhmDxeEnpSqvP5E1oEh7fMRfOlnzqNQAjqLD1Dqc3ZnrrxG1cdgg6CF4BSj8fwEqwatqPvYsUcWrlt4pRrCcgk5Rw+Awpk3KhsuyeBPWNvqHiap3mGsSJitmTRZl6TcsAWlx1Wa0FyssLSPnEu4h8Jxt48c/QE9ghpmAtpzHiKqPrQAGKDoA6c4NBFtmSEKRa1K+y7ygSeScgys0v4uTY7ySljCESrIO2rEl9PcEnS2MCamajRNy+xOG3UPm90SUO/4mIB5bkNlFsd6QhKUz9WPITspIUV/aoPtWEs9rld//UJm8Jbk8h8lcZOQPr503ThXH2IioKd4mrL+DejaXpq1xURbAAAAAElFTkSuQmCC" alt="">,k为测试的轮数
//里面有龟速乘和快速幂,防越界
inline int slow_mult(int a,int b,int mod){
int ans=;
while(b){
if(b&) ans=(ans+a)%mod;
a=(a+a)%mod;
b>>=;
}
return ans;
}
inline int quick_pow(int a,int b,int mod){
int ans=;
while(b){
if(b&) ans=slow_mult(ans,a,mod);
a=slow_mult(a,a,mod);
b>>=;
}
return ans;
}
inline bool Miller_Rabin(int p){
if(p==) return true;
if(p==||p%==) return false;
int u=p-,t=;
while(u%==) u/=,t++;
for(int i=;i<=;i++){//我测了十次
int a=rand()%(p-)+;
int x=quick_pow(a,u,p);
if(x==) continue;
for(int j=;j<=t;j++){
int y=slow_mult(x,x,p);
if(y== && x!= && x!=p-) return false;//二次探测定理
x=y;//递推求2的幂
}
if(x!=) return false;//费马小定理
}
return true;
}