euclid's algorithm

I have known it since when I was in the primary school. However, I did not get its underlying insight until now, although it is simple.

First, a brief description of the algorithm: Input, (a,b) Output: g, the greatest common divisor.

1) M<-a, N<-b, Divide M by N, suppose the remainder is r. 

2) If r==0, return N.

3) else, M<-N, N<-r, repeat the process of 1.


The step 1 and 3 give the relation that M=q*N+r. 

If a divisor is for M and N, then it is also a divisor for M-qN=r. (basic rule of division)

In the other direction, if a divisor is for N, and r, then, it is also for N and N*q +r=M.

So, the above two rules say, a divisor is for M and N <=> it is a divisor for N and r. (invariant of the recursion)

Therefore, instead of computing the divisor of M and N, we can compute the divisor of a pair of smaller numbers N and r.


The proof for why the number returned by step 2 is the greatest divisor is as follows.

Suppose the second last step is M=q'N+r. and the last step is N=q''r, accordingly (for consistent reference of the variables, we do not replace them as in step 3).

Clearly, r is the common divisor of N, r,and then M. Therefore, by recursion, it is a common divisor of a and b. As r is a common divisor, it is a divisor of g, i.e., r<=g.

For the greatest common divisor, g, it is divisor of a and b, then recursively, it is a divisor of M and N, and r. We see, r>=g.

Combining the above two relations, r=g.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值