Brief discussion on the principle and simple application of Stein algorithm for greatest common divisor (GCD)

Source: Internet
Author: User
Tags bitwise gcd greatest common divisor

Stein algorithm process and its simple proof 1. General steps: S1: When both numbers are even, divide them by 2 to at least one number of odd numbers, and the product of all common factor 2 that is recorded is k;s2: if there is still an even number, divide consecutively by 2 until the number is odd; S3: Use the more subtractive method ( GCD (A, B) =gcd (a-b,b) to find out two odd greatest common divisor d;s4: The original two number of greatest common divisor is d*k;2. Simple proof: S1: That is, to find out the largest common factor k;s2 of the power of two number 2: when the two numbers of a singular and a couple after simplification, Obviously the odd number is no even factor, then the other degenerate even after all even-numbered factors can not be the original two of the maximum common factor factor; S3: Find out the original two number is not 2 of the power of the maximum common factor D;S4: The largest common factor is 2 of the power of the maximum common factor k multiplied by the power of not 2 the maximum common factor D ; 3. Advantages of the Stein algorithm: Euclidean algorithm in the processing of small numbers when the advantage is obvious, but for large integers, high-precision division and take-rest operation is very complex, so the advantage of the Stein algorithm is that only the shift (bit operation) and subtraction operations, compared to the first two for easy to write 4. A simple description of the correlation bit operator: (1) bitwise AND (&): A&x is the binary form of the logarithmic a bitwise operation, that is, the X-bit in the binary form of a. One important application here is that a&1 can be used to judge the parity of a number a, that is, a last 0 is even, the last 1 is the odd. (2) XOR operation (^): specific introduction to the previous essay: http://www.cnblogs.com/COLIN-LIGHTNING/p/8298554.html; the application is two number of exchanges: A^=b,b^=a,a^=b that completes two number of exchanges. (3) Bitwise left SHIFT (<<): A<<=x is to make a multiplied by 2 of the X power, the principle is to let a binary form of the left X-bit, applied to the power of the 2 and multiply it to make the operation faster and more convenient; (4) Bitwise Right SHIFT (>>): a>>= X is the power of X to divide a by 2, the principle is to move the binary form of a to the right X-bit, and to divide the power of the 2 to make the operation faster and more convenient; 5. General code: (1) Recursive form:
int stein(int a,int b){    if(a<b) a^=b,b^=a,a^=b;        //交换,使a为较大数;     if(b==0)return a;                    //当相减为零,即两数相等时,gcd=a;     if((!(a&1))&&(!(b&1))) return stein(a>>1,b>>1)<<1;   //s1,注意最后的左移,在递归返回过程中将2因子乘上;    else if((a&1)&&(!(b&1)))return stein(a,b>>1);            //s2;    else if((!(a&1))&&(b&1))return stein(a>>1,b);    else return stein(a-b,b);                                             //s3;  
(2) Iteration form:
int stein(int a,int b){    int k=1;            while((!(a&1))&&(!(b&1))){    //s1;        k<<=1;                          //用k记录全部公因子2的乘积 ;        a>>=1;                                 b>>=1;    }     while(!(a&1))a>>=1;              //s2;    while(!(b&1))b>>=1;    if(a<b) a^=b,b^=a,a^=b;        //交换,使a为较大数;     while(a!=b){                           //s3;        a-=b;        if(a<b) a^=b,b^=a,a^=b;       }     return k*a;}

Brief discussion on the principle and simple application of Stein algorithm for greatest common divisor (GCD)

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.