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

  • If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop.
  • If B = 0 then GCD(A,B)=A, since the GCD(A,0)=A, and we can stop.
  • Write A in quotient remainder form (A = B⋅Q + R)
  • Find GCD(B,R) using the Euclidean Algorithm since GCD(A,B) = GCD(B,R)

这里Q是正整数.

Example:

Find the GCD of 270 and 192

  • A=270, B=192
  • A ≠0
  • B ≠0
  • Use long division to find that 270/192 = 1 with a remainder of 78. We can write this as: 270 = 192 * 1 +78
  • Find GCD(192,78), since GCD(270,192)=GCD(192,78)

A=192, B=78

  • A ≠0
  • B ≠0
  • Use long division to find that 192/78 = 2 with a remainder of 36. We can write this as:
  • 192 = 78 * 2 + 36
  • Find GCD(78,36), since GCD(192,78)=GCD(78,36)

A=78, B=36

  • A ≠0
  • B ≠0
  • Use long division to find that 78/36 = 2 with a remainder of 6. We can write this as:
  • 78 = 36 * 2 + 6
  • Find GCD(36,6), since GCD(78,36)=GCD(36,6)

A=36, B=6

  • A ≠0
  • B ≠0
  • Use long division to find that 36/6 = 6 with a remainder of 0. We can write this as:
  • 36 = 6 * 6 + 0
  • Find GCD(6,0), since GCD(36,6)=GCD(6,0)

A=6, B=0

  • A ≠0
  • B =0, GCD(6,0)=6

So we have shown:

GCD(270,192) = GCD(192,78) = GCD(78,36) = GCD(36,6) = GCD(6,0) = 6

GCD(270,192) = 6

应用:

int gcd(int a, int b) {
while(b){
int r = a % b;
a = b;
b = r;
}
return a;
}

Greatest common divisor(gcd)的更多相关文章

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

    一 暴力枚举法 原理:试图寻找一个合适的整数i,看看这个整数能否被两个整形参数numberA和numberB同时整除.这个整数i从2开始循环累加,一直累加到numberA和numberB中较小参数的一 ...

  2. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  3. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  4. 845. Greatest Common Divisor

    描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...

  5. 2018CCPC桂林站G Greatest Common Divisor

    题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...

  6. CCPC2018 桂林 G "Greatest Common Divisor"(数学)

    UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...

  7. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  8. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  9. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

随机推荐

  1. HDU_2028——求多个数的最小公倍数

    Problem Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最 ...

  2. 形形色色的软件生命周期模型(4)——MSF、实用型

    摘要: 读大学时,我们曾经学习过不少软件生命周期模型,当时还不是很懂软件开发,你可能会觉得这些东西很新奇.在实际工作中,你会发现这些模型其实很难应用,与此同时你会接触到RUP.MSF等权威软件公司的生 ...

  3. 关于XMLEncoder和XMLDecoder

    我们用XMLEncoder和XMLDecoder来序列化和反序列化一个类. 我觉得需要注意的是,我们在new一个对象的时候,XMLEncoder本身默认的是类中无参的构造函数,我今儿在实现的时候,老是 ...

  4. win7配置简单的FTP服务器

    1.开启一些win7默认是关闭状态的功能 步骤:控制面板 -> 程序和功能 -> 打开或关闭Windows功能,然后勾选下图中圈起来的部分: 2.添加FTP站点 步骤:桌面的计算机图标 - ...

  5. MyBatis第一个项目示例

    1.创建一个Java project,JikeBook 2.添加项目所需的依赖包 如 mybatis-3.2.8.jar,是实现mybatis功能所必需的依赖包 mysql-connector-jav ...

  6. XSHELL使用技巧总结

    1. 配置鼠标右键直接黏贴,选中直接复制(和putty行为一致) 工具->选项->键盘和鼠标,向右按钮 2. 如何在不同的选项卡切换 查看->会话选项卡

  7. mysql中文名字按首字母排序

    在mysql数据库中可以使用GBK编码对中文进行排序,如名字按首字母排序 order by convert(substr(tu.username,1,1) using 'GBK') 其中substr方 ...

  8. 互联网程序设计c++

    地址:ftp.sist.stdu.edu.cn用户名:lzh_hlw20133密码:lzhstdftp端口:2014

  9. [React] React Router: Router, Route, and Link

    In this lesson we'll take our first look at the most common components available to us in react-rout ...

  10. Android 基于Netty的消息推送方案之对象的传递(四)

    在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...