The art of computer programming chapter1

来源:互联网 发布:知世风涧澈 编辑:程序博客网 时间:2024/05/20 03:45

 Algorithm:extended Euclidean algorithm and its implementation

Description of Algorithm :
Given two positive integers m and n, we compute their greatest common divisor d and two integer a and b such that am+bn=d.

step 1, (Initiation] Set a'=b=1, a=b'=0, c=m, d=n;
step 2, (Divide) Let q and r be the quotient and reminder respectively,of c divided by d
step 3, (Reminder zero?)If r=0,then algorithm terminates; we have in this case am+bn=d as desired
step 4, (Recycle)Set c=d. d=r, t=a', a'=a, a=t-qa, t=b'/b, b=t-qb, and go back to step 2

proof:
Click Here    The proof of extended Euclidean algorithm and its implementation

implementation of the algorithm
First Method: use recursive method

Second Method: use non-recursive method