The Euclidean Algorithm

Clear[a1, b1]

Enter the two integers as  a1 =   and   b1 =

a1 = 852b1 = 381

Clear[n,  a,  b,  q,  r, quotients, equations,  eqn, sol,  sub]Print["The Euclidean Algorithm applied to ",  a1,  " and ",  
      b1, ":"] n = 1; °\(a = a1; 
b = b1; 
q = Quotient[a, b]; 
quotients = {q}; 

    r = \ Mod[a, b]; 
remainders = {r}; 

    Print[a, \ " = ", \ b, \ " * ", \ q, \ " + ", \ r]\)While[r > 0,  a = b; 
b = r; 
      
q = Quotient[a, b]; 
      
AppendTo[quotients,  q]; 
      
r =  Mod[a, b]; 
      
AppendTo[remainders,  r]; 
      
n = n + 1; 
      
Print[a,  " = ",  b,  "*",  q,  
        " + ",  r]]Print["gcd(",  a1,  ", ",  b1 ") = ",  
      remainders[[n - 1]]]Clear[r]

The GCD as a Linear Combination of the Numbers

\(r[n - 1] = gcd; 
r[0] = B; 
r[\(-1\)] = A; 
k = 1; 
equations = {}; 
    
While[k < n, 
      AppendTo[equations, 
        r[k - 2] == quotients[\([k]\)]*r[k - 1] + r[k]]; 
      
k = k + 1]\)Do[eqn[k] = equations[[k]],  {k, 1, n - 1}]Do[Print[eqn[k]],  {k, 1, n - 1}]

Do[sol[k] = Solve[eqn[k], r[k]], {k, 1, n - 1}]Do[Print[sol[k]], {k, 1, n - 1}]

\(sub[0] = Last[Last[sol[n - 1]]]; 

    Do[sub[j] = Last[sub[j - 1] /. sol[n - 1 - j]], {j, 1, n - 2}]\)Do[Print[sub[j]], {j, 0, n - 2}]result = Simplify[sub[n - 2]]combo = gcd /. %

Print["That is, the GCD ", remainders[[n - 1]], " = (", 
      Coefficient[combo, A], ") * ", a1, " + (", 
      Coefficient[combo, B], ") * ", b1]

ExtendedGCD[852,  381]