Current location - Recipe Complete Network - Dinner recipes - Find the greatest common divisor of two integers m and n
Find the greatest common divisor of two integers m and n

On finding the greatest common divisor of two integers m and n is shared as follows:

The Greatest Common Divisor (abbreviated gcd) is the largest positive integer that can simultaneously divide a given two or more integers.

Solving the greatest common divisor is a fundamental problem in number theory and has wide applications in computer science, informatics, cryptography, and other fields. To get the greatest common divisor of two integers m and n, you can use a variety of methods, the following are two commonly used methods:

1, rolling subtraction: rolling subtraction is an ancient Greek mathematician Euclid proposed a way to find the greatest common divisor. The specific operation is as follows:

First, compare the size of m and n, assign the larger one to a, and the smaller one to b. Calculate the difference between a and b, and assign it to t. If t is equal to 0, then b is the greatest common divisor; if t is not equal to 0, replace the original (a,b) with (a,t), and then repeat the steps 2~4 until t is equal to 0. Tumbling Subtraction has the time complexity of O(n^2 ) and is less efficient when dealing with large numbers.

2, rolling division: rolling division is an algorithm based on the inference of Euclid's theorem to find the greatest common divisor, also known as Euclid's algorithm. The core idea of Euclid's algorithm is to use the remainder of the recursion, so that the original problem is constantly shrinking, and the solution of the original problem is equivalent to the solution of the reduced problem. The operation is as follows:

Compare m and n, divide the larger number by the smaller number to get the quotient q and the remainder r; if r is equal to 0, the smaller number is the greatest common divisor;

If r is not equal to 0, continue the operation with the smaller number and the remainder r until the remainder is 0, at which point the smaller number is the greatest common divisor. The time complexity of tumble division is O(log n), which is more efficient when dealing with large numbers.

In summary, for finding the greatest common divisor of two integers m and n, you can use both the method of rolling over and subtracting and rolling over and dividing. However, it should be noted that for particularly large numbers, these two methods may take longer to compute, so more efficient algorithms are usually needed to solve this problem.