Euclid‘s algorithm
2016-03-01 22:42
489 查看
这算是TAOCP的读书笔记吧,这次写的主要是卷一的Euclid‘s algorithm。
以下是一些书上的摘录和C语言代码的实现:
对于给定的两个正整数m和n,找到相应的最大公约数。
(We
will have 0<=r<n.)
E2:[Is it zero?]If r=0,the algorithm terminates; n is the answer.
E3:[Reduce.]Set m<-n,n<-r,and go back to step E1.II
After step E1,we have
m=q*n+r,
for some integer q.If r=0,then m is a multiple of n,and clearly in such a case n is the greatest common divisor of m and n.If r != 0,note that any
number that divides both m and n must divide m-q*n = r,and any number
that divides both n and r must divide q*n+r = m; so the set of common divisors of {m,n} is the same as the set of common divisors of {n,r}.
对于 m 和 n 有 m = q * n + r ;
当 r 为 0 时,m 和 n 有公约数 q ;
当 r 不为 0 时,设 m 和 n 的公约数是 d ,则有(m - q * n)能被 d 整除, 那么 r 也能被 d 整除。又因为 r = m%n ,所以 GDC(m,n)=GCD(n,m%n)。直到 r 为 0 时,就是最大公约数 GCD(m,n)。
鉴于本人英语能力的不足,如有不当之处希望各位大神指点。
以下是一些书上的摘录和C语言代码的实现:
算法要求:
Give two positive integers m and n, find their greatest common divisor,that is,the largest positive integer that evenly divides both m and n.对于给定的两个正整数m和n,找到相应的最大公约数。
描述1:
E1:[Find remainder.]Divide m by n and let r be the remainder.(We
will have 0<=r<n.)
E2:[Is it zero?]If r=0,the algorithm terminates; n is the answer.
E3:[Reduce.]Set m<-n,n<-r,and go back to step E1.II
描述2:
int Eucild(int m,int n) { r=m%n; while(r) { m=n,n=r; r=m%n; } return n; }
证明:
After step E1,we havem=q*n+r,
for some integer q.If r=0,then m is a multiple of n,and clearly in such a case n is the greatest common divisor of m and n.If r != 0,note that any
number that divides both m and n must divide m-q*n = r,and any number
that divides both n and r must divide q*n+r = m; so the set of common divisors of {m,n} is the same as the set of common divisors of {n,r}.
对于 m 和 n 有 m = q * n + r ;
当 r 为 0 时,m 和 n 有公约数 q ;
当 r 不为 0 时,设 m 和 n 的公约数是 d ,则有(m - q * n)能被 d 整除, 那么 r 也能被 d 整除。又因为 r = m%n ,所以 GDC(m,n)=GCD(n,m%n)。直到 r 为 0 时,就是最大公约数 GCD(m,n)。
鉴于本人英语能力的不足,如有不当之处希望各位大神指点。
相关文章推荐
- SGOI 海上搜索 状压DP
- Django基础——Form&Ajax篇
- 算法代码实现之堆排序,Golang(Go语言)实现
- 【CodeForces】A. Dragons
- 熟悉一下GO的AES加密库
- Django~static files
- Django中的F表达式来解决丢失修改问题
- 关于Go语言共享内存操作的小实例
- Xcode警告Category is implementing a method which will also be implemented by its primary class 处理
- POJ2195 Going Home(SPFA最小费用最大流)
- go liteIDE
- Django 路由
- django:field字段类型
- mongo2.4_point_in_time_recovery
- USACO 2016 Jan Gold 愤怒的奶牛(Angry Cows)
- 如何在WindowsAzure上搭建Mean stack环境之如何在中国版Azure上搭MangoDB
- google perftools分析程序性能
- django1.9的static文件配置
- 减少集成google-play-service-lib后编译的apk的大小
- Go语言三大框架点评