13.07.2015 Views

제 2 ‚절 The Euclidean Algorithm

제 2 ‚절 The Euclidean Algorithm

제 2 ‚절 The Euclidean Algorithm

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2 THE EUCLIDEAN ALGORITHM 72003 9 09 2 <strong>The</strong> <strong>Euclidean</strong> <strong>Algorithm</strong> 1.12 (<strong>The</strong> <strong>Euclidean</strong> <strong>Algorithm</strong>). x, y (x ≥ y > 0) r 0 = x, r 1 = y . 0 Division algorithm r j = r j+1 q j+1 + r j+2 ,(0 ≤ j ≤ n − 2 0 ≤ r j+2 < r j+1 r n+1 = 0) , x, y gcd(x, y) = r n .. Division algorithm r 0 = r 1 q 1 + r 2 , (0 ≤ r 2 < r 1 )r 1 = r 2 q 2 + r 3 , (0 ≤ r 3 < r 2 )· · ·r i−2 = r i−1 q i−1 + r i (0 ≤ r i < r i−1 )· · · r i , 0 . , n r n+1 = 0. Division algorithm 1.11 (x, y) = (r 0 , r 1 ) = (r 1 , r 2 ) = · · · = (r n−1 , r n ) = (r n , r n+1 ) = (r n , 0) = r n . 1.4. <strong>Euclidean</strong> algorithm gcd(252, 198) .Maple Program 3. [MyGCD( )]<strong>Euclidean</strong> algorithm procedure.: MyGCD(n,m)


8 1 > MyGCD:=proc(n,m)> local temp1, temp2, remainder, mygcd;> temp1:=min(abs(n),abs(m));> temp2:=max(abs(n),abs(m));> if temp1=0 then> mygcd:=temp2;> else> remainder:=temp2 mod temp1;> while (remainder0) do> temp2:=temp1;> temp1:=remainder;> remainder:=temp2 mod temp1;> end do;> mygcd:=temp1;> end if;> mygcd;> end proc;MyGCD := proc(n, m)local temp1 , temp2 , remainder, mygcd;temp1 := min(abs(n), abs(m)) ;temp2 := max(abs(n), abs(m)) ;if temp1 = 0 then mygcd := temp2elseremainder := temp2 mod temp1 ;while remainder ≠ 0 dotemp2 := temp1 ; temp1 := remainder ; remainder := temp2 mod temp1end do;mygcd := temp1end if;mygcdend proc> MyGCD(109861564297,444307556492);333331Maple Program 4. [recurGCD( )]Procedure procedure.: recurGCD(n,m).


2 THE EUCLIDEAN ALGORITHM 9> recurGCD:=proc(n,m) option remember;> local temp1, temp2;> temp1:=min(abs(n),abs(m));> temp2:=max(abs(n),abs(m));> if temp1 = 0 then> temp2;> else> recurGCD(temp1,temp2 mod temp1);> end if;> end proc;recurGCD := proc(n, m)local temp1 , temp2 ;option remember;temp1 := min(abs(n), abs(m)) ;temp2 := max(abs(n), abs(m)) ;if temp1 = 0 then temp2 else recurGCD(temp1 , temp2 mod temp1 ) end ifend proc> recurGCD(109861564297,444307556492);333331 1.5. , maple procedure . 1.6. n , mapleprocedure .(Hint: procedure nargsi args[i] .) 1.9 x, y m, n gcd(x, y) =mx + ny . , 252 198 1818 = gcd(252, 198) = 4 · 252 + (−5) · 198 4 -5 . m n . 1.13 (Extended <strong>Euclidean</strong> <strong>Algorithm</strong>). x, y x ≥ y > 0 . r i q i 1.12 gcd(x, y) = r n , (1.1) s i , t i gcd(x, y) = s n · x + t n · y


10 1 .s i = s i−2 − q i−1 s i−1 , (2 ≤ i ≤ n)s 0 = 1,s 1 = 0,t i = t i−2 − q i−1 t i−1 , (2 ≤ i ≤ n)t 0 = 0,t 1 = 1.(1.1). s i , t i (1.1) , 1 ≤ i ≤ n r i = s i x + t i y (1.2) . r 0 = 1 · x + 0 = s 0 x + t 0 y r 1 = 0 · x + 1 · y =s 1 x + t 1 y i 0 1 . 2 ≤ i ≤ k − 1 (1.2) . <strong>Euclidean</strong> algorithm k r k−2 = r k−1 q k−1 + r k , r k = r k−2 − r k−1 q k−1= (s k−2 x + t k−2 y) − (s k−1 x + t k−1 y)q k−1= (s k−2 − s k−1 q k−1 )x + (t k−2 − t k−1 q k−1 )y= s k x + t k y (1.2) i = k . , . Maple Program 5. [extGCD( )] n, m gcd(n, m) sn + tm =gcd(n, m) s, t , L[1], L[2], L[3] gcd(n, m), s, t list .: A:=extGCD(n,m) extGCD(n,m).> restart;


2 THE EUCLIDEAN ALGORITHM 11> extGCD:=proc(x,y)> local extGCD_pos,L;> extGCD_pos:=proc(n,m)> local r0, r1, r2, s0, s1, s2, t0, t1, t2, q1;> r0:=n;> r1:=m;> s0:=1;> s1:=0;> t0:=0;> t1:=1;> q1:=floor(evalf(r0/r1));> r2:=r0 mod r1;> while (r2 > 0) do> s2:=s0-q1*s1;> s0:=s1;> s1:=s2;> t2:=t0-q1*t1;> t0:=t1;> t1:=t2;> r0:=r1;> r1:=r2;> q1:=floor(evalf(r0/r1));> r2:=r0 mod r1;> end do;> [r1,s1,t1];> end proc;> if (x=0 and y0) then> L:=[y,0,1];> elif (x0 and y=0) then> L:=[x,1,0];> elif (x L:=[L[1],-L[2],-L[3]];> elif (x0) then> L:=extGCD_pos(-x,y);> L:=[L[1],-L[2],L[3]];> elif (x>0 and y L:=extGCD_pos(x,-y);> L:=[L[1],L[2],-L[3]];> elif (x>0 and y>0) then> L:=extGCD_pos(x,y);> L:=[L[1],L[2],L[3]];> end if;> printf("%d=(%d*%d)+(%d*%d)\n",L[1],L[2],x,L[3],y);> L;> end proc;


12 1 extGCD := proc(x, y)local extGCD pos, L;extGCD pos := proc(n, m)local r0 , r1 , r2 , s0 , s1 , s2 , t0 , t1 , t2 , q1 ;r0 := n ;r1 := m ;s0 := 1 ;s1 := 0 ;t0 := 0 ;t1 := 1 ;q1 := floor(evalf(r0 /r1 )) ;r2 := r0 mod r1 ;while 0 < r2 dos2 := s0 − q1 ∗ s1 ;s0 := s1 ;s1 := s2 ;t2 := t0 − q1 ∗ t1 ;t0 := t1 ;t1 := t2 ;r0 := r1 ;r1 := r2 ;q1 := floor(evalf(r0 /r1 )) ;r2 := r0 mod r1end do;[r1 , s1 , t1 ]end proc;if x = 0 and y ≠ 0 then L := [y, 0, 1]elif x ≠ 0 and y = 0 then L := [x, 1, 0]elif x < 0 and y < 0 then L := extGCD pos(−x, −y) ; L := [L 1 , −L 2 , −L 3 ]elif x < 0 and 0 < y then L := extGCD pos(−x, y) ; L := [L 1 , −L 2 , L 3 ]elif 0 < x and y < 0 then L := extGCD pos(x, −y) ; L := [L 1 , L 2 , −L 3 ]elif 0 < x and 0 < y then L := extGCD pos(x, y) ; L := [L 1 , L 2 , L 3 ]end if;printf(“%d=(%d*%d)+(%d*%d) \”, L 1 , L 2 , x, L 3 , y);Lend proc> A:=extGCD(-24524578924,24598245424);‘4=(1611284665*-24524578924)+(1606459211*24598245424)‘A := [4, 1611284665, 1606459211]


2 THE EUCLIDEAN ALGORITHM 13> A[1];4> A[2];1611284665> A[3];1606459211> save extGCD, "extGCD";

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!