用欧几里得算法求最大公约数_欧几里得算法:GCD(最大公约数),用C ++和Java示例解释...

本文介绍了如何使用欧几里得算法来计算两个或更多整数的最大公约数(GCD),包括了GCD的基本概念、MOD操作的解释,以及算法的详细步骤。还提供了C++、Java和JavaScript的递归代码示例,并提及了扩展欧几里得算法用于求解贝祖特恒等式的系数。
摘要由CSDN通过智能技术生成

用欧几里得算法求最大公约数

For this topic you must know about Greatest Common Divisor (GCD) and the MOD operation first.

对于本主题,您必须首先了解最大公约数(GCD)和MOD操作。

最大公约数(GCD) (Greatest Common Divisor (GCD))

The GCD of two or more integers is the largest integer that divides each of the integers such that their remainder is zero.

两个或多个整数的GCD是将每个整数相除以使它们的余数为零的最大整数。

Example-GCD of 20, 30 = 10  (10 is the largest number which divides 20 and 30 with remainder as 0)GCD of 42, 120, 285 = 3  (3 is the largest number which divides 42, 120 and 285 with remainder as 0)

Example-GCD为20、30 = 10 (10是将20和30除以0的最大数) ,42、120、285 = 3 (3是将42、120和285除以余数的最大数) 0)

“ mod”操作 ("mod" Operation)

The mod operation gives you the remainder when two positive integers are divided. We write it as follows-A mod B = R

当两个正整数相除时,mod操作为您提供余数。 我们将其编写如下: A mod B = R

This means, dividing A by B gives you the remainder R, this is different than your division operation which gives you the quotient.

这意味着,用A除以B得到的余数为R,这与除以商的商不同。

Example-7 mod 2 = 1  (Dividing 7 by 2 gives the remainder 1)42 mod 7 = 0  (Dividing 42 by 7 gives the remainder 0)

示例7 mod 2 = 1 (将7除以2得到余数1) 42 mod 7 = 0 (将42除以7得到余数0)

With the above two concepts understood you will easily understand the Euclidean Algorithm.

了解了以上两个概念后,您将轻松理解欧几里得算法。

欧几里德最大公约数算法(GCD) (Euclidean Algorithm for Greatest Common Divisor (GCD))

The Euclidean Algorithm finds the GCD of 2 numbers.

欧几里得算法找到2个数字的GCD。

You will better understand this Algorithm by seeing it in action. Assuming you want to calculate the GCD of 1220 and 516, lets apply the Euclidean Algorithm-

通过查看实际效果,您将更好地理解该算法。 假设您要计算1220和516的GCD,请应用欧几里得算法-

Assuming you want to calculate the GCD of 1220 and 516, lets apply the Euclidean Algorithm-

假设您要计算1220和516的GCD,请应用欧几里得算法-

Pseudo Code of the Algorithm-Step 1:  Let  a, b  be the two numbersStep 2:  a mod b = RStep 3:  Let  a = b  and  b = RStep 4:  Repeat Steps 2 and 3 until  a mod b  is greater than 0Step 5:  GCD = bStep 6: Finish

算法的伪代码-步骤1: a, b为两个数字步骤2: a mod b = R步骤3: a = bb = R步骤4: 重复步骤2和3,直到a mod b更大大于0步骤5: GCD = b步骤6:完成

JavaScript Code to Perform GCD-

执行GCD-JavaScript代码

function gcd(a, b) {
  var R;
  while ((a % b) > 0)  {
    R = a % b;
    a = b;
    b = R;
  }
  return b;
}

JavaScript Code to Perform GCD using Recursion-

使用递归执行GCDJavaScript代码-

function gcd(a, b) {
  if (b == 0)
    return a;
  else
    return gcd(b, (a % b));
}

C code to perform GCD using recursion

使用递归执行GCD的C代码

int gcd(int a, int b) 
{ 
    // Everything divides 0  
    if (a == 0) 
       return b; 
    if (b == 0) 
       return a; 
  
    // base case 
    if (a == b) 
        return a; 
  
    // a is greater 
    if (a > b) 
        return gcd(a-b, b); 
    return gcd(a, b-a); 
}

C++ Code to Perform GCD-

执行GCD-的C ++代码

int gcd(int a,int b) {
  int R;
  while ((a % b) > 0)  {
    R = a % b;
    a = b;
    b = R;
  }
  return b;
}

Python Code to Perform GCD using Recursion

使用递归执行GCD的Python代码

def gcd(a, b):
  if b == 0:
    return a:
  else:
    return gcd(b, (a % b))

Java Code to Perform GCD using Recursion

Java代码使用递归执行GCD

static int gcd(int a, int b)
{
  if(b == 0)
  {
    return a;
  }
  return gcd(b, a % b);
}

You can also use the Euclidean Algorithm to find GCD of more than two numbers. Since, GCD is associative, the following operation is valid-  GCD(a,b,c) == GCD(GCD(a,b), c)

您也可以使用欧几里得算法来查找两个以上的GCD。 由于GCD是关联的,因此以下操作有效-GCD GCD(a,b,c) == GCD(GCD(a,b), c)

Calculate the GCD of the first two numbers, then find GCD of the result and the next number. Example-  GCD(203,91,77) == GCD(GCD(203,91),77) == GCD(7, 77) == 7

计算前两个数字的GCD,然后找到结果和下一个数字的GCD。 GCD(203,91,77) == GCD(GCD(203,91),77) == GCD(7, 77) == 7

You can find GCD of  n  numbers in the same way.

您可以用相同的方法找到n数字的GCD。

什么是扩展欧几里得算法? (What is the Extended Euclidean Algorithm?)

This is an extension of Euclidean algorithm. It also calculates the coefficients x, y such that

这是欧几里得算法的扩展。 它还计算系数x,y,这样

ax+by = gcd(a,b)

ax + by = gcd(a,b)

x and y are also known as coefficients of Bézout's identity.

x和y也称为贝索特恒等式的系数。

c code for Extended Euclidean algorithm

扩展欧几里得算法的C代码

struct Triplet{
	int gcd;
	int x;
	int y;
};
Triplet gcdExtendedEuclid(int a,int b){
	//Base Case
	if(b==0){
		Triplet myAns;
		myAns.gcd = a;
		myAns.x = 1;
		myAns.y = 0;
		return myAns;

	}
	Triplet smallAns = gcdExtendedEuclid(b,a%b);
	//Extended euclid says

	Triplet myAns;
	myAns.gcd = smallAns.gcd;
	myAns.x  = smallAns.y;
	myAns.y = (smallAns.x - ((a/b)*(smallAns.y)));
	return myAns;	
}

翻译自: https://www.freecodecamp.org/news/euclidian-gcd-algorithm-greatest-common-divisor/

用欧几里得算法求最大公约数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值