Euclid‘s algorithm

这算是TAOCP的读书笔记吧,这次写的主要是卷一的Euclid‘s algorithm。


以下是一些书上的摘录和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 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)。

鉴于本人英语能力的不足,如有不当之处希望各位大神指点。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值