Fast power-take modulus algorithm

Source: Internet
Author: User
Tags modulus

Reference article Source: reait Home (http://www.reait.com/blog.html) Reprint Please specify, thank you for your cooperation.
Fast power modulus is a common algorithm, which is summarized here.
A^b%c (This is known as the RSA public Key encryption method), when a is large, directly solve the problem is not likely
Algorithm 1: Using the formula a*b%c= ((a%c) *b)%c, this processing is done at each step, which solves the problem that the a^b may be too large, but the time complexity of the algorithm is still not optimized
The code is as follows:

int Mod1 (int a,int b,int N)     {        int cnt = 1;    while (b--)    {        cnt = a * cnt% n;    }    return CNT;}  </span>
algorithm 2: Another algorithm uses two points of thought, can reach O (logn).
where P (i) (0<=i<=n) is 0 or 1

so a^b =  a^  (P (n) *2^n  +  p (n-1) *2^ (n-1)   +...+  P (1) *2  +  p (0))
                =  a^ (P (n) *2^n)   *  a^ (P (n-1) *2^ (n-1))   *...*  a^ (P (1) *)   *  a^p (0)
for the case of P (i) =0, a^ (P (i) * 2^ (i-1)  ) =  a^0  =  1, no processing
We have to consider only P (i) =1 the case (This is important!!) For details, see Qin Jiushao algorithm:Http://baike.baidu.com/view/1431260.htm)
With this, we can recursively calculate all the a^ (2^i)
Of course, by the conclusion of algorithm 1, we add modulo operations:
a^ (2^i)%c = ((a^ (2^ (i-1))%c) * a^ (2^ (i-1)))%c
Then the a^ (2^i)%c that satisfies P (i) =1 is multiplied by the algorithm 1 and%c is the result , that is, the binary scan has been scanned from the highest bit to the lowest bit .Instance code: Recursive
int Mod2 (int a,int b,int n) {    int t = 1;    if (b = = 0)        return 1;    if (b = = 1)    return a%n;    T=mod2 (A, b>>1, n);    t=t*t%n;    if (b&1)    {        t = t*a% n;    }    return t;}
Example code 2: Non-recursive optimizations:
int Mod3 (int a,int b,int y) {    int cnt=1;    while (b)    {        if (b&1) cnt=cnt*a%y;        a=a*a%y;        b>>=1;    }    return CNT;}





Fast power-take modulus algorithm

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.