美文网首页
Miller-Rabin

Miller-Rabin

作者: fo0Old | 来源:发表于2018-10-21 21:21 被阅读0次

http://miller-rabin.appspot.com/

struct MillerRabin
{
    const int base[14]=
    {0,2,3,5,7,11,13,17,19,23,29,31,37,41};

    static ll qmul(ll x,ll y,ll mod)
    {
        if(x>=mod)x%=mod;
        if(y>=mod)y%=mod;
        ll res=0;
        for(;y;y>>=1)
        {
            if((y&1) && (res+=x)>=mod)
                res-=mod;
            if((x+=x)>=mod)x-=mod;

        }
        return res;
    }

    static ll qpow(ll x,ll y,ll mod)
    {
        if(x>=mod)x%=mod;
        ll res=1;
        for(;y;y>>=1,x=qmul(x,x,mod))
            if(y&1)res=qmul(res,x,mod);
        return res;
    }

    bool test(ll n)
    {
        if(n==2)return true;
        if(n<=1 || !(n&1))return false;
        ll u;for(u=n-1;!(u&1);u>>=1);
        for(int i=1;i<=13 && base[i]<n;++i)
        {
            ll x=qpow(base[i],u,n),y;
            for(ll v=u;v<=n;x=y,v<<=1)
            {
                y=qmul(x,x,n);
                if(y==1 && x!=1 && x!=n-1)
                    return false;
            }
            if(x!=1)return false;
        }
        return true;
    }
}MR;

相关文章

  • Miller-Rabin

    http://miller-rabin.appspot.com/

  • Miller-Rabin素数测试

    转载自Matrix大牛一个数是素数(也叫质数),当且仅当它的约数只有两个——1和它本身。规定这两个约数不能相同,因...

  • Miller-Rabin素数测试

    费马小定理:对于素数p和任意整数a,有ap ≡ a(mod p)(同余)。反过来,满足ap ≡ a(mod p),...

  • Python 进行高精度运算

    gmpy2是Python的一个扩展库,可以进行高精度运算,适用于Miller-Rabin素数测试算法,大素数生成,...

  • Miller-Rabin(米勒罗宾)素性测试

    算法思想 对于大于2的素数n,将n-1拆分为 准确性 所有的奇合数都有很多的a满足"witness"的条件,不过目...

网友评论

      本文标题:Miller-Rabin

      本文链接:https://www.haomeiwen.com/subject/smeizftx.html