Greatest common divisor(gcd)
欧几里得算法求最大公约数
- 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)的更多相关文章
- 最大公约数Greatest Common Divisor(GCD)
一 暴力枚举法 原理:试图寻找一个合适的整数i,看看这个整数能否被两个整形参数numberA和numberB同时整除.这个整数i从2开始循环累加,一直累加到numberA和numberB中较小参数的一 ...
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- 2018CCPC桂林站G Greatest Common Divisor
题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...
- CCPC2018 桂林 G "Greatest Common Divisor"(数学)
UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
随机推荐
- 一句话输出NGINX日志访问IP前十位排行
AWK的数组字段自增加,然后取值的方法,要记得哟. 还有,SORT指定列排行,也常用的.
- PullToRefreshListView组件的OnItemClickListener中的position下标问题
/** * Pass-through method for {[url=home.php?mod=space&uid=91636]@link[/url] PullToRefreshBase#g ...
- shell中使用sqlplus及调试相关
五.为了安全要求每次执行shell都手工输入密码$ vi test5.sh #!/bin/bashecho -n "Enter password for u_test:"read ...
- Android中程序包的相关操作
//获取系统中已经安装的应用程序 List<PackageInfo> packageinfos=this.getPackageManager().getInstalledPackages( ...
- 读<<如何阅读一本书>>乱七八糟的笔记1
阅读层次 第一层:基础阅读 第二层:检视阅读 系统化略读 第三层:分析阅读 第四层:主题阅读(比较阅读) 第二层:检视阅读 1.有系统的粗读或略读 (1)先看书名页,然后如果有序就先看序 (2)研究目 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- 提升效率的Linux终端快捷操作汇总
很多普通 Linux 桌面用户都对使用终端感到排斥和恐惧,其实它没大家想的那么复杂,很多常见操作都可以直接在终端中进行,如:安装软件.升级系统等. 无论你是新手还是 Linux 终端使用的老鸟,系统极 ...
- [置顶] API相关工作过往的总结之整体介绍
此系列的总结文章,仅仅是我个人工作总结,有考虑不周之处还请各位同行多多指教. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是 ...
- Jmeter压力测试环境准备
Jmeter性能监控 配置好测试机器上的jmeter环境:http://jmeter-plugins.org/downloads/all/ 网站下载两个东西: JMeterPlugins-Standa ...
- Linux 与 BSD 有什么不同?
Linux 与 BSD 有什么不同? 这篇文章是别人写的,并做了一点修改. 汉澳sinox就是基于bsd开发的,因此能够理解为一个bsd分支,可是由于sinox不开源,被排除在外.bsd不是商业软件, ...