Two Fast GCD Algorithms

Two Fast GCD Algorithms

Two Fast GCD Algorithms

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. Two Fast GCD Algorithms JONATHAN SORENSON Department of Mathematics and Computer Science Butler University Journal of algorthms 報告者：張圻毓

2. Outline • The right-shift Binary • The right-shift k-ary GCD • The left-shift Binary • The left-shift k-ary GCD • Experiment conclusion

3. The right-shift Binary • 1.g=1 while u and v are both even do • 2. g=2g ,u=u/2 ,v=v/2 • 3. while u≠0 and v ≠0 do • 4. if u is even,u=u/2 • 5. else if v is even,v=v/2 • 6. else max{u,v}=|u-v|/2 return(g(u+v)) • 濃縮版 • 1. if u and v are both even, gcd(u,v)=2gcd(u/2,v/2) • 2. if u is even and v is odd, gcd(u,v)=gcd(u/2,v) • 3. if u and v are both odd, gcd(u,v)=gcd(|u-v|/2,v) • 4. gcd(u,0)=u

4. The right-shift k-ary GCD • 1.while u≠0 and v≠0 do: • 2. if gcd(u,k)>1 ,u=u/gcd(u,k) • 3. else if gcd(v,k)>1,v=v/gcd(v,k) • 4. else • 5. find nonzero integers a,b satisfying • au+bv=0 (mod k) • 6. max{u,v}=|au+bv|/k

5. Example • u=561 v=627 取掉K=7以下的GCD為3 • u=209 v=187 k=7 • 209 mod k=6 ,187 mod k=5 • a=5 ,b=-6 使得滿足5. • |5u-6v|/7=11 • u=11 v=187k=7 • 11 mod k=4 , 187 mod k=5 • a=5, b=-4 • |5u-4v|/7=99

6. Example • u=11 v=99 k=7 • 11 mod k=4 ,99 mod k=1 • a=-1 ,b=4 • |-1u+4v|/7=55 • u=11 v=55 • 11 mod k=4 , 55 mod k=6 • a=-6, b=4 • |-6u+4v|/7=22

7. Example • u=11 v=22 k=7 • 11 mod k=4 ,22 mod k=1 • a=-1 ,b=4 • |-1u+4v|/7=11 • u=11 v=11 • 11 mod k=4 , 11 mod k=4 • a=1, b=-1 • |u-v|/7=0 • u=11 v=0 11*3=33 GCD=33

8. The left-shift Binary • 1.if u<v then swap(u,v) • 2.while v≠0 do • 3. compute t=2ev such that t≦u<2t • 4. u=min{u-t,2t-u} • 5. if u<v then swap(u,v) • 6.return(u); • 濃縮版 • 1.gcd(u,v)=gcd(|u+bv|,v),while b is any interger • 2.gcd(u,0)=u

9. Example • u=627 v=561 • e=0 t=2ev=561 t≦u<2t => 561 ≦627<2*561 • u=min{u-t,2t-u}=66 • u=561 v=66 • e=3 t=23*66 528≦561<16*66 • u=min{u-t,2t-u}=33 • u=66 v=33 • e=1 t=2*33=66 66≦66<2*66 • u=min{u-t,2t-u}=0 • u=33 v=0

10. The left-shift k-ary GCD • 1.if u<v then swap(u,v) • 2.while v≠0 do • 3. compute t=kev such that t≦u﹤kt • 4. find a,b≦k such that t/u=b/a • u=|at-bu| • 5. if u<v then swap(u,v)

11. Example • u=561 v=627 取掉K=7以下的GCD為3 • u=209 v=187 • e=0 t=187 187≦209<7*187 演算法3. • t/u=187/209≒0.89…≒1/1=b/a • u=|1*t-1*u|=22 • u=187 v=22 • e=1 t=7*22=154 154 ≦187<7*154 • t/u=154/187≒0.82…≒1/1=b/a • u=|1*t-1*u|=33

12. Example • u=33 v=22 • e=0 t=22 22≦33<7*22 • t/u=22/33≒0.66…≒3/4=b/a • u=|4*t-3*u|=11 • u=22 v=11 • e=0 t=11 11 ≦22<7*11 • t/u=11/22=0.5=1/2=b/a • u=|2*t-1*u|=0 • u=11 v=011*3=33 GCD=33

13. Experiment conclusion