描述

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

  • In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers.

样例

Given a = 10, = 15, return 5.

Given a = 15, b = 30, return 15.

辗转相除法, 又名欧几里德算法(Euclidean algorithm),是求最大公约数的一种方法。它的具体做法是:用较小数除较大数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,如此反复,直到最后余数是0为止。如果是求两个数的最大公约数,那么最后的除数就是这两个数的最大公约数。
另一种求两数的最大公约数的方法是更相减损法。
public class Solution {
/**
* @param a: the given number
* @param b: another number
* @return: the greatest common divisor of two numbers
*/
public int gcd(int a, int b) {
// write your code here if(b == 0) {
return a;
}
return gcd (b, a%b); }
}

845. Greatest Common Divisor的更多相关文章

  1. [UCSD白板题] Greatest Common Divisor

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

  2. greatest common divisor

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

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

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

  4. hdu 5207 Greatest Greatest Common Divisor 数学

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

  5. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

  6. 【Leetcode_easy】1071. Greatest Common Divisor of Strings

    problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...

  7. 2018CCPC桂林站G Greatest Common Divisor

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

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

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

  9. leetcode 1071 Greatest Common Divisor of Strings

    lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...

随机推荐

  1. 【python】mongo删除数据

    参考:https://stackoverflow.com/questions/23334743/setting-justone-limiter-for-pymongo-remove-throws-ty ...

  2. JSP Filters(过滤器)

    Filter是拦截Request请求的对象:在用户的请求访 问资源前处理ServletRequest以及ServletResponse,它可 用于日志记录.加解密.Session检查.图像文件保护 等 ...

  3. MySQL报错: Character set ‘utf8mb4‘ is not a compiled character set and is not specified in the ‘/usr/share/mysql/charsets/Index.xml‘ file

    由于日常程序使用了字符集utf8mb4,为了避免每次更新时,set names utf8mb4,就把配置文件改了,如下: [root@~]# vim /etc/my.cnf #my.cnf [clie ...

  4. redis 单实例安装

    单实例安装 近些年,由于内存技术的提升.造价的下降,越来越多企业的服务器内存已增加到几百G.这样的内存容量给了内存数据库一个良好的发展环境. 而使用Redis是内存数据库的一股清流,渐有洪大之势.下面 ...

  5. 【C++ Primer | 03】字符串、向量和数组

    博客链接: c++ 中 const_iterator 和 const vector<>::iterator的区别 const vector <int> ::iterator和v ...

  6. 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化,而报【远程服务器返回错误: (500) 内部服务器错误】

    1. 修改web.config文件可以改变这个默认值(上传1个G) <configuration> <system.web> <httpRuntime maxReques ...

  7. 在WPF中获取DATAGRIDTEMPLATECOLUMN模板定义的内容控件(转载)

    原文:http://www.cnblogs.com/eric_ibm/p/3772516.html xaml格式描述: <DataGrid Name="dataGrid" G ...

  8. 435. Non-overlapping Intervals

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  9. WPF数据爬取小工具-某宝推广位批量生成,及订单爬取 记:接单最痛一次的感悟

    项目由来:上月闲来无事接到接到一个单子,自动登录 X宝平台,然后重定向到指定页面批量生成推广位信息:与此同时自动定时同步订单数据到需求提供方的Java服务. 当然期间遇到一个小小的问题就是界面样式的问 ...

  10. 【译】如何使用Vue捕获网络摄像头视频

    几个月前,我一直关注着比特币的爆发并且在GDAX网站上注册账号.在注册验证的过程中,网站提示要通过计算机的网络摄像头提交我自己的一张照片作为照片ID.这是一个很酷的做法,让我思考一个问题:在网络浏览器 ...