欧几里得gcd c++
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20.
两个正整数的最大公约数(gcd)是它们的最大公约数。 让我们考虑两个数字12和20。
The divisors of 12 are 1, 2, 3, 4, 6, 12
12的因数是1,2,3,4,6,12
The divisors of 20 are 1, 2, 4, 5, 10 20
20的因数是1,2,4,5,10 20
The highest number among the common divisors of 12 and 20 is 4. The gcd of 12 and 20 is 4. We'll start finding our answer by taking minimum of 12 and 20 which is 12. We'll return the highest number between 12 and 1 which is divided by 12 and 20. The steps are given below:
在12和20的公约数中,最大数为4。12和20的gcd为4。我们将通过取最小的12和20(即12)开始寻找答案。我们将返回12之间的最大数。和1除以12和20。步骤如下:
Is 12 divided by 12 and 20? No, decrement 12 by 1 which is 11
将12除以12和20? 不,将12减1即11
Is 11 divided by 12 and 20? No, decrement 11 by 1 which is 10
将11除以12和20? 不,将11减1即10
Is 10 divided by 12 and 20? No, decrement 10 by 1 which is 9
10除以12和20? 不,将10减1即9
..........................
.................................... ..... ..... ..... ..... ..... ..... ..... ......
..........................
.................................... ..... ..... ..... ..... ..... ..... ..... ......
Is 5 divided by 12 and 20? No, decrement 5 by 1 which is 4
5除以12和20? 不,将5减1即为4
Is 4 divided by 12 and 20? Yes, we return 4 as our answer.
4除以12和20? 是的 ,我们返回4作为答案。
The implementation of the above algorithm is -
上述算法的实现是-
public int gcd(int a, int b) {
for(int div = minimum(a, b); div >= 1; --div) {
if(a % div == 0 && b % div == 0) {
return div;
}
}
}
The divisors of 540 are 1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 27, 30, 36, 45, 54, 60, 90, 108, 135, 180, 270, 540
的540除数为1,2,3,4,5,6,9,10,12,15,18,20,27,30,36,45,54,60,90,108,135,180,270 540
The divisors of 600 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24 25, 30, 40, 50, 60, 75, 100, 120, 150, 200, 300, 600
的600除数为1,2,3,4,5,6,8,10,12,15,20,24 25,30,40,50,60,75,100,120,150,200,300, 600
The above algorithm is not optimal for this example because it performs many comparisons (starting from 540, decrementing by 1 for each iteration down to 60) to find the actual solution. So, the iteration keeps going (540, 539, 538, .........., 60) until we get 60.
上面的算法对于此示例不是最佳的,因为它执行了许多比较(从540开始,每次迭代递减1,直到60为止)以找到实际的解决方案。 因此,迭代持续进行(540,539,538,..........,60),直到得到60。
There is another approach given below which is known as Euclidean algorithm. In this approach, we consider (600, 540) as a pair and divide the larger number by smaller one. This forms a new pair (540, 60) where 540 is the smaller number of previous pair and 60 is the difference between previous pair. This process repeats until one value of the pair becomes 0. This approach is faster and more efficient than the previous approach.
下面给出了另一种称为欧几里得算法的方法 。 在这种方法中,我们将(600,540)视为一对,并将较大的数除以较小的数。 这形成一个新的对(540,60),其中540是前一对的较小数,而60是前一对之间的差。 重复此过程,直到该对中的一个值变为0。此方法比以前的方法更快,更有效。
540 | 600 | 1
540
-----------------
60 | 540 | 9
540
------------------
X
That is, gcd(600, 540) = gcd(540, 600 mod 540)
= gcd(540, 540 mod 60)
= gcd(60, 0)
= 60
public int gcdEucledian(int a, int b) {
while(b > 0) {
int temp = a % b;
a = b;
b = temp;
}
}
翻译自: https://www.experts-exchange.com/articles/17329/Finding-GCD-Euclidean-Algorithm.html
欧几里得gcd c++