二进制欧几里德算法


Euclid's algorithm is tersely expressed by the recursive formula 
(1) gcd(N,M) = gcd(M, N mod M), 

where (N mod M) is the remainder of division of N by M. We postulate gcd(N,0) = N in accordance with the end condition of Euclid's algorithm. Our example appears as 
gcd(2322,654) = gcd(654,360) = gcd(360,294) = gcd(294,66) = gcd(66,30) = gcd(30,6) = gcd(6,0) = 6. 

Other properties of gcd are expressed in such a similarly concise form 

1. gcd(KN, KM) = K gcd(N, M) 
2. If gcd(N,M) = 1 then gcd(N,MK) = gcd(N,K) 
3. gcd(N, M) = gcd(N - M, M) 


There are many ways to prove these. For instance, the first and second follow from the Fundamental Theorem of Arithmetic; the second (in a more direct manner) is also a consequence of a generalization of Proposition VII.30 the third one follows from the basic properties of modular arithmetic and division. A binary algorithm for finding gcd(N,M) is based on the following 

1. If N and M are even, gcd(N, M) = 2 gcd(N/2, M/2), 
2. If N is even while M is odd, then gcd(N, M) = gcd(N/2, M), 
3. If both N and M are odd, then (since N-M is even) |N-M| < max(N,M). Replace the largest of the two with |N-M|. 

The algorithm is known as binary because, unlike the original one, it does not use general division of integers but only division by 2. Since in a computer numbers are represented in the Binary system anyway, you should not be surprised to learn that there is a special machine instruction that implements division by 2 in a highly efficient manner. This is known as the right shift wherein the rightmost bit is discharged, the remaining bits are shifted one place to the right and the leftmost bit is set to 0. 

Another handy operation is a bitwise conjunction &. N & 1 is either 1 or 0 for any integer N. It's 1 iff N is odd. Bitwise conjunction is also implemented in hardware, i.e., as a machine instruction. 

The binary algorithm was discovered by R.Silver and J.Tersian in 1962 and has been published by G.Stein in 1967. Convergence of the algorithm, if not obvious, can be shown by induction. Property 3. above assures that induction is applicable.
from:http://www.cut-the-knot.org/blue/binary.shtml 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值