欧几里得gcd c++_寻找GCD(欧几里得算法)

欧几里得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 1234, 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
Euclidean algorithm is
欧几里得算法是


public int gcdEucledian(int a, int b) {
	while(b > 0) {
		int temp = a % b;
		a = b;
		b = temp;
	}
}
Please keep in mind that efficiency of algorithm really matters when we consider a very big number. The first approach will take forever to find the gcd of a pair that has a long long integer equal to or greater than 9,223,372,036,854,775,803. As a user would you ever use a calculator that takes 30 seconds or more to calculate gcd?
请记住,当我们考虑数量很大时,算法的效率确实很重要。 第一种方法将永远花费时间查找长等于或大于9,223,372,036,854,775,803的长整数对的gcd。 作为用户,您是否会使用花费30秒或更长时间计算gcd的计算器?

翻译自: https://www.experts-exchange.com/articles/17329/Finding-GCD-Euclidean-Algorithm.html

欧几里得gcd c++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值