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

  • 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. MYSQL正式环境主从复制(不锁表,不停服务)

    参考URL: http://rfyiamcool.blog.51cto.com/1030776/1016636/ 原因源于其实以前环境是有MYSQL主从复制的,且最开始主从复制之间是OK的. 但由于日 ...

  2. 【转】android如何查看cpu的占用率和内存泄漏

    原文网址:http://www.cnblogs.com/yejiurui/p/3472765.html 在分析内存优化的过程中,其中一个最重要的是我们如何查看cpu的占用率和内存的占用率呢,这在一定程 ...

  3. WIN7笔记本显示连接不可用

    如图所示,找不到任何无线网络 解决方法: 不小心把无线关掉而已,笔记本键盘上F5~F12找看看有没有无线标志,有的话要先按下 Fn键不放,再按下那个有无线标志的Fn键.

  4. ASP.NET MVC framework 学习

    http://www.cnblogs.com/lmfeng/archive/2013/03/28/2986123.html  MVC数据绑定方式 http://www.cnblogs.com/lmfe ...

  5. POJ1743---Musical Theme (后缀数组+二分)

    题意 :求最长不相交重复子串的长度.. 可以先求出LCP,,对于长度 二分出答案..(竟然不会写二分) #include <set> #include <map> #inclu ...

  6. Javascript 精髓整理篇之二(函数篇)postby:http://zhutty.cnblogs.com

    今天总结的内容是javascript的function, 涉及到function顺便讲讲this. Function 是javascript的函数,也是js的执行单元.函数是JavaScript的一种 ...

  7. Linux 下文件监控

    本文转自http://www.jiangmiao.org/blog/2179.html 在日常应用中,常常会遇到以下场景,监控文件夹A,若文件夹中的B文件发生变化,则执行C命令.Linux下可以通过i ...

  8. 经常使用ASCII码表(方便查找)

    经常使用ASCII码表(方便查找) 键盘 ASCII码 键盘 ASCII码 键盘 ASCII码 键盘 ASCII码 ESC 27 7 55 O 79 g 103 SPACE 32 8 56 P 80 ...

  9. 移动web开发框架研究

    纯粹的总结一下移动web开发框架,移动web开发框架有jQuery Mobile .Sencha Touch等等,他们都来源于web开发,是成熟的框架.jQuery Mobile出自于jQuery家族 ...

  10. 对return 语句的正确性和效率进行检查

    注意事项如下: 1. return 语句不可返回指向"堆栈内存“的”指针“或者”引用“,因为该内存单元在函数体结束时被自动释放. //错误 char* Func(void) { char s ...