To find the minimum number of conventions, the most easy to think of is Euclid algorithm, the algorithm is relatively easy to understand, the efficiency is very good. Also known as the Euclidean method.
For any two number A, a (a>b), D=GCD (A, A, b), gcd (b) =gcd (B,A%B)
Proof: Make r=a%b, that exists K, make a=b*k+r, then r=a-b*k; obviously R>=0, r%d= ((a%d)-(b*k)%d)%d, because a%d=b%d=0, so r%d=0;
So GCD (A, A, b) can be transferred to GCD (b,a%b), then this is a recursive process, then when the end of the recursion, think about, a, a, a can not be zero, then the second is zero, as the end of recursion (of course, can also be terminated with other conditions), This is the method of seeking greatest common divisor with other end conditions), which is the method of seeking greatest common divisor.
Euclid's Recursive version:
int gcd (int a,int b) { if (b==0) return A; else return gcd (b,a%b);}
Non-recursive version:
int gcd (int a,int b)//euclid{ int R; while (b!=0) { r=a%b; a=b; b=r; } return A;}
Stein AlgorithmEuclidean algorithm is a traditional algorithm to calculate two numbers greatest common divisor, he is good both in theory and in efficiency. But he has a fatal flaw, and this flaw will only manifest in large primes. Hardware platform, the general integer is at most 64 bits, for such an integer, it is very simple to calculate the modulus between two numbers. For a platform with a word length of 32 bits, the modulo of two integers with no more than 32 bits is required, only one instruction period is needed, and the integer modulus of 64 bits or less is calculated, but only a few cycles. But for larger prime numbers, such a process has to be designed by the user, in order to calculate two more than 64-bit integer modulo, the user may have to use similar to the multi-digit division hand calculation process of the trial law, the process is not only complex, and consumes a lot of CPU time. For modern cryptographic algorithms, the need to calculate more than 128 prime numbers abound, the design of such a program is eager to abandon division and modulo. Stein algorithm by J. Stein in 1961, this method is also a calculation of the two-digit greatest common divisor. Unlike Euclid's algorithm, the Stein algorithm only shifts and adds and subtracts integers, which is a boon for program designers. To illustrate the correctness of the Stein algorithm, we must first note the following conclusions:
GCD (a,a) = A, that is, a number and the number of his own conventions is its own
GCD (ka,kb) = K gcd (A, b), that is, the greatest common divisor operation and the multiplier operation can be exchanged, special, when k=2, indicating that two even-numbered greatest common divisor must be divisible by 2
When k cannot be divisible by B,GCD (ka,b) =GCD (A, b), that is, only one of the two numbers has a factor that does not affect greatest common divisor. In particular, when k=2, the description calculates an even and an odd number of greatest common divisor, you can divide the even number by 2.
Algorithm steps:
1. If a=0,b is greatest common divisor, the algorithm ends
2. If B=0,a is greatest common divisor, the algorithm ends
3. Set A1=a, B1=b and C1=1
4, if an and Bn are even, then an+1=an/2,bn+1=bn/2,cn+1=cn*2 (note, multiply 2 to move the integer to the left one can, except 2 to move the integer right one can)
5, if an is even, Bn is not even, then AN+1=AN/2,BN+1=BN,CN+1=CN (obviously, 2 is not an odd number of approximate)
6, if the Bn is even, an is not even, then BN+1=BN/2,AN+1=AN,CN+1=CN (obviously, 2 is not an odd number of approximate)
7, if an and bn are not even, then an+1=| An-bn|/2,bn+1=min (an,bn), CN+1=CN
8, N plus 1, ext. 1
Better understand it, the implementation is relatively simple, efficiency is not worse than the algorithm;
Here's the code for the implementation:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm >using namespace Std;int stein (int a,int b) { if (a==0) return B; if (b==0) return A; if (a%2==0 && b%2==0) return 2*stein (a>>1,b>>1); else if (a%2==0) return Stein (A>>1,B); else if (b%2==0) return Stein (a,b>>1); else return Stein (ABS (a), Min (A, b));} int main () { int a A, B; scanf ("%d%d", &a,&b); printf ("%d\n", Stein (A, b));}
Minimum number of conventions (Euclidean algorithm &&stein algorithm)