当前位置:网站首页>Rabin Miller prime test

Rabin Miller prime test

2022-06-11 07:36:00 CaptainHarryChen

This paper does not prove the correct probability , Because I won't
Determine whether large integers are prime numbers

Premise theorem

  1. Fermat's small Theorem : if n n Prime number , a < n , be an11 (mod n) a n − 1 ≡ 1   ( m o d   n ) ; If any integer x x Satisfy a x 1 1   ( m o d   x ) , be x x There is a high probability that it is a prime number ;
  2. Theorem 2: if p For more than 2 The prime number of , be a21 (mod p) a 2 ≡ 1   ( m o d   p ) The solution of can only be a1 a ≡ 1 or a1 a ≡ − 1 ;

Rabin-Miller Prime number test

If the number to be measured n n For more than 2 The prime number of , be n 1 For the even
set up n1=2s×d n − 1 = 2 s × d
Random one less than n n The integer of a
a2s×d1 (mod n) a 2 s × d ≡ 1   ( m o d   n )
(a2s1×d)21 (mod n) ( a 2 s − 1 × d ) 2 ≡ 1   ( m o d   n )
a2s1×d1 (mod n) a 2 s − 1 × d ≡ 1   ( m o d   n ) or a2s1×d1 (mod n) a 2 s − 1 × d ≡ − 1   ( m o d   n )
if 1, You can also continue recursion , Until get -1 Or get ad±1 (mod n) a d ≡ ± 1   ( m o d   n )

The implementation , Turn it upside down
First, calculate ad a d , Judge whether it is ±1 ± 1
And then keep squaring , obtain a2×d,a22×d,a23×d...a2s×d a 2 × d , a 2 2 × d , a 2 3 × d . . . a 2 s × d
Until one step in the middle gets 1 − 1 , Pass the test .
If you get -1 Before , Got it 1, It means that the number is not equal to ±1 The square of the number of gets 1, Not satisfied with the theorem 2, Then the number test fails

Generally, you have to pick a lot at random a a , Test many times to improve the probability .

But in OI in , Use this string a That's it
{ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 }
It guarantees 3,825,123,056,546,413,051 3 , 825 , 123 , 056 , 546 , 413 , 051 The number within is calculated correctly ( Has more than long long)

Code

Edition title :HDU2138

#include<cstdio>
const int TEST_VAL[]={
   2,3,5,7,11,13,17,19,23,29,31,37,41},TEST_NUM=13;

long long PowMod(long long a,long long b,long long p)
{
    long long res=1LL;
    while(b)
    {
        if(b&1LL)
            res=1LL*res*a%p;
        a=1LL*a*a%p;
        b>>=1LL;
    }
    return res;
}

bool MillerRabin(long long a,long long n,long long d)
{
    long long x=PowMod(a,d,n);
    if(x==1||x==n-1)
        return true;
    while(d<n-1)
    {
        x=1LL*x*x%n;
        d=d*2LL;
        if(x==1)
            return false;
        if(x==n-1)
            return true;
    }
    return false;
}
bool isPrime(long long x)
{
    if(x<10)
    {
        if(x==2||x==3||x==5||x==7)
            return true;
        return false;
    }
    long long d=x-1;
    while((d&1LL)==0)
        d>>=1LL;
    for(int i=0;i<TEST_NUM;i++)
        if(TEST_VAL[i]<x&&!MillerRabin(TEST_VAL[i],x,d))
            return false;
    return true;
}

int main()
{
    int n,ans;
    long long x;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        while(n--)
        {
            scanf("%I64d",&x);
            ans+=isPrime(x);
        }
        printf("%d\n",ans);
    }

    return 0;
}
原网站

版权声明
本文为[CaptainHarryChen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110723241037.html