Euclid's GCD Algorithm——MIT Open Courseware(Computability)
2015-06-27 21:10
477 查看
Euclid’s GCD algorithm
An example of ancient computational thinking, a really wonderful non-obvious efficient algorithm is Euclid’s GCD algorithm. It starts with this question.How do you reduce a fraction like 510/646 to lowest terms?
Well, we know we need to take out the greatest common divisor (GCD). How do you do that? The grade school method is to factor both numbers; the product of all the common prime factors is the GCD, and we simply cancel them out. But we said before that factoring is believed to be hard. The brute force method is OK for grade school problems, but it won’t do for thousand-digit numbers. But just like testing whether a number is prime or composite, it turns out that the GCD problem can be solved in a different, more clever way—one that doesn’t require factoring.
Euclid’s clever observation was that if a number divides two numbers, say 510 and 646, it also divides any integer linear combination of them, say 646 − 510. [Do you see why this is so?] In general, when we divide B by A, we get a quotient q and a remainder r connected by the equation B = qA + r, which means that r = B − qA, which means that r is a linear combination of A and B!
So finding the GCD of 510 and 646 is the same as finding the GCD of 510 and the remainder when we divide 646 and 510. This is great because the remainder is a smaller number. We’ve made progress!
GCD(510, 646) = GCD(136, 510)
And we can keep doing the same thing.
GCD(136, 510) = GCD(102, 136) = GCD(34, 102) = 34
Here we stopped because 34 divides 102, and we know this means that 34 is the GCD of 34 and 102. We could also take it a step further and appeal to the fact that the GCD of any number and 0 is that number: GCD(34, 102) = GCD(0, 34) = 34.
Pseudo-code:
GIVEN: natural numbers A, B
Assume B is the larger (otherwise swap them)
If A is 0 return B
Else find the GCD of (B % A) and A
Above is from MIT Open Courseware materials. Something about
computability.
Java implement
/**
* @Author: heartsuit
* @DateTime: 2015-06-27 19:35:21
* @Function: Euclid’s GCD Algorithm
* How do you reduce a fraction like 510/646 to lowest terms?* @Instruction: GCD: The greatest common divisor
* Note: the input arguments must be natural numbers, i.e. the negtive numbers are invalid!
*/
public class GCD {
public int getGCD_Euclid(int a, int b) {
// Assume b is the larger one
if (a > b) {
int temp = a;
a = b;
b =
b998
temp;
}
// Recursion
if (a == 0) {
return b;
} else {
return getGCD_Euclid(a, b%a);
}
}
public int getGCD_WithoutSwap(int a, int b) {
// Recursion
if (a == 0) {
return b;
} else {
return getGCD_WithoutSwap(b%a, a); //Note the order of input arguments
}
}
public int getGCD_WithoutRecursion(int a, int b) {
while(a != 0) {
int remainder = b % a;
b = a;
a = remainder;
}
return b;
}
public static void main(String[] args) {
GCD gcd = new GCD();
// Test cases
int[] a = new int[]{0, 1, 6, 0, 510, 646};
int[] b = new int[]{0, 1, 0, 6, 646, 510};
for (int i = 0; i < a.length; i++) {
System.out.print(gcd.getGCD_Euclid(a[i], b[i]) + " ");
System.out.print(gcd.getGCD_WithoutSwap(a[i], b[i]) + " ");
System.out.print(gcd.getGCD_WithoutRecursion(a[i], b[i]));
System.out.println();
}
}
}
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!
相关文章推荐
- linux命令
- apache+php+mysql环境配置
- 容器+AOP实现动态部署(四)
- 11个超棒的iOS开发学习网站
- Sqoop处理Clob与Blob字段
- openlayers3 在地图上叠加WFS查询矢量图层
- linux 环境变量设置方法总结(PATH/LD_LIBRARY_PATH)
- 不能再docker容器中使用宿主机的docker服务的解决方案
- Openlayers3 计算地图上任意两点间的距离
- 容器+AOP实现动态部署(三)
- 虚拟机Linux----Ubuntu1204----安装jdk1.8
- linux常用命令的英文单词缩写
- Linux图形界面与字符界面切换
- Hadoop优化 第一篇 : HDFS/MapReduce
- J2EE编辑中的中文乱码解决问题
- [Linux]在终端启动程序关闭终端不退出的方法
- 通过在shell脚本中用scp或rsync实现远程同步文件
- Bash中单引号和双引号的区别
- linux下一个eclipse组态jdk
- popupWindow使用详解