1, two number coprime: if the two number of the public factor is only 1, then it can be said that the two numbers coprime.
Euclidean algorithm for greatest common divisor:
First, greatest common divisor, suppose we ask for a and b 's greatest common divisor
Set a mod b = C;
You can get a recursive process:
A = kb + C;
Assuming A, B 's greatest common divisor is D, you can get:
A = MD, b = nd;
m, n coprime;
c = a-kb = Md-knd = (m-kn) D;
We already know m,n coprime, then we can know n and m-kn coprime, then C and b The greatest common divisor is also D;
So by the above inference, we can get,a , b 's greatest common divisor equals b and a mod b 's greatest common divisor, Recursive iterative operations, until the two numbers are equal, the values of a and b at this time are greatest common divisor.
int-2147483648 ~ +2147483647 (4 Bytes)
unsigned int 0 ~ 4294967295 (4 Bytes)
Long = = Int
Long long-9223372036854775808 ~ +9223372036854775807 (8 Bytes)
Euclidean algorithm to find the limitations of greatest common divisor:
In the application of greatest common divisor, if the above method is used to find the greatest common divisor, if we want to greatest common divisor the large integer, the efficiency of the method of dividing is a certain problem, in fact, for large integers, the time cost of dividing a large integer is very expensive, which is the limitation of the method of dividing.
Workaround:
Can learn from Euclid's Euclidean method, since it is the problem of modulo operation, then we do not have to take the modulo operation, the "-" operation, that is, f (x, y) =f (x-y,y); Consider the possibility of x<y in the process of running the algorithm, this time to Exchange X and Y, But the results are unaffected. Let's take a look at the code.
BigInt GCD (BigInt x,bigint y)
{
if (x < y) return GCD (y,x);
Return (!y)? X:GCD (X-y,y);
}
The bigint used in the code is a large integer class that stores hundreds or thousands of bits of integers. How does this class come true? There is not much to say, specific online has a lot of relevant articles to explain the high-precision algorithm, if you look at books, highly recommended ACM Daniel Rujia, "Algorithmic Competition Primer Classic" A book, the high-precision introduction is very good.
The problem with large numbers is solved, however, the careful reader will find that this algorithm introduces another problem, that is, when X and Y are much different, the number of iterations of the algorithm will be too high, resulting in less efficient algorithm, such as Computing gcd (100000000000001,1). The situation does exist, so we have to consider other optimizations.
Two-digit least common multiple = two-digit flight/greatest common divisor;
Euclidean algorithm for greatest common divisor + least common multiple