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/ ...
随机推荐
- 俄罗斯方块:Python实现
网上搜到一个Pygame写的俄罗斯方块(tetris),大部分看懂的前提下增加了注释,Fedora19下运行OK的 主程序: #coding:utf8 #! /usr/bin/env python # ...
- POJ1743---Musical Theme (后缀数组+二分)
题意 :求最长不相交重复子串的长度.. 可以先求出LCP,,对于长度 二分出答案..(竟然不会写二分) #include <set> #include <map> #inclu ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- HDU-3787(字符串模拟)
Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出. Input 输入包含 ...
- linux loadavg详解(top cpu load)
目录 [隐藏] 1 Loadavg分析 1.1 Loadavg浅述 1.2 Loadavg读取 1.3 Loadavg和进程之间的关系 1.4 Loadavg采样 2 18内核计算loadavg存在的 ...
- script标签的位置
1.在我们编写代码的时候,会在页面内使用<script>标签来写JS,虽然理论上script标签的位置放在哪里可以,但是还是有一点区别的. 2.为什么很多人把script标签放在底部: 初 ...
- android4.4 settings 中控制卡1 卡2都振动
在package/app/Settings/src/com/android/settings/SoundSettings.java
- Java static块
首先,我们看一个实际例子: class Test{ public static int X=100; public final static int Y=200; public Test(){ Sys ...
- 第三章 Android绘图机制与处理技巧
1.屏幕尺寸信息 屏幕大小:屏幕对角线长度,单位“寸”:分辨率:手机屏幕像素点个数,例如720x1280分辨率:PPI(Pixels Per Inch):即DPI(Dots Per Inch),它是对 ...
- hdu 1042
貌似之前也写过这个题目的解题报告...老了,记性不好 从贴一遍吧! 代码理解很容易 AC代码: #include <iostream> #include <stdio.h> # ...