Evaluate the maximum common divisor (ansi c) using the Stein Algorithm)

Source: Internet
Author: User
First, we introduce a symbol: g_c_d is the abbreviation of greatest common divisor (maximum common divisor). g_c_d (x, y) indicates the maximum common divisor of X and Y. Then we need to know that all the dikes of an odd number are odd. This is very easy. We will use it below.
To study the properties of the maximum common divisor, we found that g_c_d (K * X, K * y) = K * g_c_d (x, y) this is a very good nature (proving that I am saving it ). He said that he is good because he is very in line with our small thoughts. Let's try K = 2, then g_c_d (2x, 2y) = 2 * g_c_d (x, y ). This quickly reminds us of the method of narrowing two even numbers. So how can we reduce the odd and odd numbers?

Let's take a look at the odd case: there are two numbers: 2X and Y, where Y is an odd number. Because all the approx. Values of Y are odd, A = g_c_d (2x, Y) is odd. It is not difficult to think of 2X as an even number. A should be the approximate number of X. Let's prove: (2x) % A = 0, set 2X = N * a, because a is an odd number and 2x is an even number, then n must be an even number. And because X = (n/2) * a, X % A = 0, that is, a is the approximate number of X. Because a is also the approximate number of Y, a is the approximate number of X and Y, with g_c_d (2x, Y) <= g_c_d (x, y ). G_c_d (X, Y) is obviously the common divisor of 2X and Y, and g_c_d (x, y) <= g_c_d (2x, Y), so g_c_d (2x, Y) = g_c_d (x, y ). So far, we have come up with an odd, even small method.

Let's take a look at two odd numbers: There are two odd numbers X and Y. It seems that there is no good way to convert x and y directly to small numbers. We can take a detour, move X and Y closer to the even number to make them smaller. Let's set X> Y. We noticed that X + Y and x-y are two even numbers, and g_c_d (x + y, x-y) exists) = 2 * g_c_d (x + y)/2, (x-y)/2), then g_c_d (x, y) and g_c_d (x + y, x-y) is there a certain relationship between g_c_d (x + y)/2, (x-y)/2? For the convenience of setting M = (x + y)/2, n = (x-y)/2, it is easy to find m + n = x, M-N = y. If a = g_c_d (m, n) is set, M % A = 0, N % A = 0, so (m + n) % A = 0, (m-N) % A = 0, that is, X % A = 0, Y % A = 0, so a is the approximate number of X and Y, with g_c_d (m, n) <= g_c_d (X, Y ). If B = g_c_d (X, Y) is set to an odd number, then X % B = 0, Y % B = 0, so (x + y) % B = 0, (x-y) % B = 0, and because X + Y and x-y are both even numbers, it is the same as the method used to prove that A is an approximate number of X in the previous odd case, (x + y)/2) % B = 0, (x-y)/2) % B = 0, that is, M % B = 0, N % B = 0, so B is the approximate number of M and N, with g_c_d (x, y) <= g_c_d (m, n ). So g_c_d (x, y) = g_c_d (m, n) = g_c_d (x + y)/2, (x-y)/2 ).

Let's sort out the two positive integers x> Y:
1. All are even g_c_d (x, y) = 2g_c_d (X/2, Y/2 );
2. All values are odd g_c_d (x, y) = g_c_d (x + y)/2, (x-y)/2 );
2. x odd y even g_c_d (x, y) = g_c_d (X, Y/2 );
3. x even y odd g_c_d (x, y) = g_c_d (X/2, Y) or g_c_d (x, y) = g_c_d (Y, X/2 );

Now we have a recursive formula, and we need to find another degradation condition. Note that g_c_d (x, x) = x, we use this.

Int Stein (unsigned int X, unsigned int y)
/** // * Return the greatest common divisor of x and y */
...{
Int factor = 0;
Int temp;

If (x <Y)
...{
Temp = X;
X = y;
Y = temp;
}
If (0 = y)
...{
Return 0;
}
While (X! = Y)
...{
If (X & 0x1)
... {/** // * When X is odd */
If (Y & 0x1)
... {/** // * When X and Y are both odd */
Y = (x-y)> 1;
X-= y;
}
Else
... {/** // * When X is odd and Y is even */
Y> = 1;
}
}
Else
... {/** // * When X is even */
If (Y & 0x1)
... {/** // * When X is even and Y is odd */
X> = 1;
If (x <Y)
...{
Temp = X;
X = y;
Y = temp;
}
}
Else
... {/** // * When X and Y are both even */
X> = 1;
Y> = 1;
+ Factor;
}
}
}
Return (x <factor );
}

 

The advantages of the Stein algorithm are self-evident. The above Code only involves bitwise operation and subtraction, which not only speeds up, but also leads to the inconvenience of the Euclidean Algorithm in finding the maximum common divisor of two large numbers. Furthermore, the Stein algorithm can be easily written into assembly languages for implementation.

 

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.