最大公约数(greatest common divisor)-欧几里得(Euclid) 算法
本文地址:http://blog.csdn.net/caroline_wendy/article/details/17012455
最大公约数(欧几里得算法(Euclid's Algorithm))是比较经典的算法;
主要方法: 递归相除, 求余数, 直至余数为0, 返回最后一个除数, 即可; 这样, 最早的两个数, 就都包含此除数;
此算法不需要指定大小顺序, 当顺序相反时, 第二次的余数就是较大的数;
代码如下:
/*
* algorithms.java
*
* Created on: 2013.11.28
* Author: Spike
*/
/*eclipse kepler, javase-1.7*/
public class algorithms {
//最大公约数
public static int gcd (int p, int q)
{
if (q == 0) return p;
int r = p % q;
return gcd(q, r);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int res = gcd(42, 70);
System.out.println("Hei, the gcd is " + res + ". ");
}
}
分享到:
相关推荐
Algorithm-Problem-Solving-with-Algorithms-and-Data-Structures-using-Python.zip,使用python的算法和数据结构解决问题的代码,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
problem-solving-with-algorithms-and-data-structure-using-python 中文版
python-algorithms-mastering-basic-algorithms-in-the-python-language.9781430232377.53502.pdf
Neo4j,用户手册,涵盖所有集成的图算法及应用场景,非常适合图算法的学习和应用
data-algorithms-book, 数据算法书的MapReduce Spark Java和 Scala 数据算法。作者:Mahmoud Parsian ( mahmoud.parsian@yahoo.com )标题:数据算法:使用Hadoop和 Spark 扩展的食谱。这个GitHub存储库将托管所有的...
Machine_Learning_Algorithms-master,Machine_Learning_Algorithms-master 配套数据集及源代码 Machine_Learning_Algorithms-master,Machine_Learning_Algorithms-master 配套数据集及源代码
MIT算法导论-Introduction to Algorithms-算法实现
Beginning Algorithms----学习算法的好书,介绍各种算法!!!
遗传算法Python程序 Hands-On-Genetic-Algorithms-with-Python-master.zip
go-algorithms - 使用golang实现不同的算法和数据结构
Machine-Learning-Algorithms-from-Scratch, 从零开始实现机器学习算法 Machine-Learning-Algorithms-from-Scratch从零开始实现机器学习算法。目前实现的算法:简单线性回归。数据集:来自Quandl的股票数据逻辑回归...
[麻省理工学院-算法导论].Introduction.to.Algorithms--PPT
Python-for-Algorithms--Data-Structures--and-Interviews, 关于算法和数据结构的Udemy课程文件 用于算法。数据结构和访谈的 python ! 欢迎访问Udemy课程的知识库: 用于算法,数据结构和访谈的python !这是为你...
算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制。也就是说,能够对一定规范的输入,在有限时间内获得所要求的输出。如果一个算法有...
算法导论第三版英文版 introduction-to-algorithms-3rd-edition
Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms...
Algorithms - Robert Sedgewick, Kevin Wayne Algorithms - Robert Sedgewick, Kevin Wayne 视频 算法 普林斯顿
本资源中包括聚类分析算法实现,基于时间序列分析的聚类算法实现,主要应用于股票时间序列等的数据分析,clustering-algorithms-master
Algorithm-Algorithms-by-S.Dasgupta.zip,试图从S.Dasgupta等人的算法中解决练习和算法实现。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Algorithm-Algorithms-and-Data-Structures-in-Java.zip,Java中的算法和数据结构,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。