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

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;elsereturn 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 Caseif(b==0){Triplet myAns;myAns.gcd = a;myAns.x = 1;myAns.y = 0;return myAns;}Triplet smallAns = gcdExtendedEuclid(b,a%b);//Extended euclid saysTriplet 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/

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

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

  1. python使用集合实现筛选法求素数-python素数筛选法浅析

    原理: 素数,指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数.在加密应用中起重要的位置,比如广为人知的RSA算法中,就是基于大整数的因式分解难题,寻找两个超大的素数然后相乘作 ...

  2. c 语言 用矩形法求定积分,如何用矩形法(梯形法)求定积分

    分析: 高中的时候,我们学习过,可以通过矩形法或者矩形法来求定积分. 思路就是将积分区间划分成n等份,然后将这n等份近似看成矩形(或梯形),然后对所有的矩形(或梯形)的面积进行求和. 简单的例子: 求 ...

  3. 【计算流体力学】Python实现加权余量法求微分方程数值解 比较伽辽金法(Galerkin法)、最小二乘法和矩法的求解精度 分析误差随n增大的变化情况

    一.简介 微分方程的经典数值方法有Ritz法.加权余量法.有限元法.有限体积法等等.对于微分方程的边值问题,如果能找到与微分方程相对应的泛函,可以通过求取相应泛函的极小值,将微分方程转化为关于基函数待 ...

  4. java 穷举法求水仙花数_常用算法-穷举法

    穷举法又称为枚举法,它是在计算机算法设计中用得最多的一种编程思想.它的实现方式是:在已知答案范围的情况下,依次地枚举该范围内所有的取值,并对每个取值进行考查,确定是否满足条件.经过循环遍历之后,筛选出 ...

  5. python筛选法求素数讲解_埃氏筛选法求素数 Python

    代码如下 def _odd_iter(): # 构建奇数序列 从3开始 n = 1 while True: n = n + 2 yield n def _not_divisible(n): retur ...

  6. python用递归方式实现最大公约数_使用Python求解最大公约数的实现方法

    1. 欧几里德算法 欧几里德算法又称辗转相除法, 用于计算两个整数a, b的最大公约数.其计算原理依赖于下面的定理: 定理: gcd(a, b) = gcd(b, a mod b) 证明: a可以表示 ...

  7. c语言写一个用矩形法求,写一个用矩形法求定积分的通用函数

    解: #include main() { float integral(float(8p)(float),float a,float b,int n); float a1,b1,a2,b2,a3,b3 ...

  8. java存储过程示例_安全密码存储–请勿做的事和Java示例

    java存储过程示例 安全存储密码的重要性 作为软件开发人员,我们最重要的职责之一就是保护用户的个人信息. 如果没有我们应用程序的技术知识,用户别无选择,只能相信我们正在履行这一责任. 令人遗憾的是, ...

  9. dijkstra标号法表格_标号法求最短路径例题详解.ppt

    标号法求最短路径例题详解 r * 最短路径 带权图G=, 其中w:E?R. ?e?E, w(e)称作e的权. e=(vi,vj), 记w(e)=wij . 若vi,vj不 相邻, 记wij =?. 设 ...

最新文章

  1. K155ID1辉光管驱动芯片功能测试
  2. 《VMware Virtual SAN权威指南》一第1章 VSAN概述
  3. java如何写对象配置文件,Java 读写Properties配置文件详解
  4. 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1068:与指定数字相同的数的个数
  5. random(随机模块)
  6. Linux 用户(User)查询篇
  7. 关于利用exchange server 2003搭建邮件服务器:小进步……
  8. centos 6.0 rpm 包编译环境的搭建
  9. php留言板验证验证码,留言板7 图形验证码
  10. JavaScript推箱子游戏开发笔记
  11. android float 百分比,如何在android中计算百分比
  12. MySQL知识点总结(二)---查询操作
  13. 软考信息系统项目管理师_合同法_著作权_实施条例---软考高级之信息系统项目管理师030
  14. D-OJ刷题日记:顺序查找 题目编号:517
  15. SystemUi概述
  16. 【GAOPS050】自同步加扰和帧同步加扰
  17. Servlet概念性回顾(结合Ajax)
  18. 磁编码器MT6835_SPI读取位置信息
  19. postgresql日常运维
  20. Vue环境搭建及第一个hello world

热门文章

  1. 【js】四种自定义对象的常见方法
  2. CSS之复合选择器(交集、并集选择器)
  3. iOS 自定义双向滑块Slider
  4. Rokid webhook 指南 手把手教你做个懒人
  5. apache 开启 gzip 压缩服务
  6. hibernate.cfg.xml详细配置
  7. tomcat虚拟路径的几种配置方法
  8. swift3.0最新拨打电话方法
  9. 大闸蟹的OO第二单元总结
  10. css:z-index