Miller_rabin algorithm (random algorithm to determine whether a number is a prime)

Source: Internet
Author: User

1 Const intS = -;//the number of random algorithms, the larger the s, the smaller the probability of the wrong judgment2ll Pow_mod (ll A, ll B, ll MoD) {//A^b%mod3LL ans =1;4A = a%MoD;5      while(b) {6         if(B &1) {7Ans = (ans * a)%MoD;8         }9A = (A * a)%MoD;Tenb >>=1; One     } A     returnans; - } - BOOLCheck (ll A, ll N, ll X, ll t) { theLL ret =Pow_mod (A, x, n); -LL last =ret; -      for(inti =1; I <= t; i++) { -RET = (ret * ret)%N; +         if(ret = =1&& Last! =1&& Last! = N-1)return true; -Last =ret; +     } A     if(Ret! =1)return true; at     Else return false; - } - BOOLMiller_rabin (Long LongN) { -     if(N <2)return false; -     if(n = =2)return true; -     if(N &1) ==0)return false; inLL x = n-1; -LL T =0; to      while((X &1) ==0 ) { +X >>=1; -t++; the     } *      for(inti =0; i < S; i++) { $LL a = rand ()% (N-1) +1;Panax Notoginseng         if(Check (A, n, x, T)) -             return false; the     } +     return true; A}

Miller_rabin algorithm (random algorithm to determine whether a number is a prime)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.