Euclidean algorithm for greatest common divisor Using the euclidean algorithm for $\gcd(121, 330)$, how many divisions are required for the process?
 A: This in Mathematica will give you the number of subtractions needed:
gcd[a_, b_] := gcd[Abs[a], Abs[b], 0];
gcd[a_, 0, step_] := {a, step};
gcd[0, b_, step_] := {b, step};
gcd[a_, b_, step_] := Which[
   a >= b, gcd[a - b, b, step + 1],
   b > a, gcd[a, b - a, step + 1]];

It's not optimized at all (it manually does each subtraction rather than using Mod) and probably what you want isn't the subtractions, but you can make modifications. It also shows you how you can make these kinds of "algorithmic" things in Mathematica by pattern matching. For example, the step in the algorithm that reads "if either number is 0, stop" is encoded in the 2nd and 3rd lines in the code.
A plot of the subtractions-required looks like this:
data = Table[gcd[i, j][[2]], {i, Range[0, 80]}, {j, Range[0, 80]}];
ListPlot3D[data, InterpolationOrder -> 2, Mesh -> None, Boxed -> False, PlotRange -> Full]


I know this is not exactly what you're looking for, but I wanted to bring in the computational perspective. :)
Update
In response to @Jyrki's comment, here is the 'faster' version of the algorithm which subtracts as many multiples as possible in a given step:
gcd[a_, b_] := gcd[Abs[a], Abs[b], 0];
gcd[a_, 0, step_] := {a, step};
gcd[0, b_, step_] := {b, step};
gcd[a_, b_, step_] := Which[
   a >= b, gcd[a - IntegerPart[a/b]*b, b, step + 1],
   b > a, gcd[a, b - IntegerPart[b/a]*a, step + 1]];

data = Table[gcd[i, j][[2]], {i, Range[0, 599]}, {j, Range[0, 599]}];

Because the algorithm is fast (Max[data] shows that any two numbers between 0 and 599 will resolve in at most 12 steps), the value jumps rapidly between only a few discrete values (0-12), which makes a regular 3D plot very noisy. So I used a density plot instead:
ListDensityPlot[data, ImageSize -> {600, 600}, PlotRangePadding -> None, Frame -> None, Axes -> False]


Here's a "close-up" achieved by plotting in a different range:
data = Table[gcd[i, j][[2]], {i, Range[250949, 250949 + 599]}, {j, Range[0, 599]}];


A: Observe that
\begin{align}
\gcd(121,330) &= \gcd(121,88) \quad (88 = 330 - 2 \times 121.) \\
              &= \gcd(33,88) \quad (33 = 121 - 1 \times 88.) \\
              &= \gcd(33,22) \quad (22 = 88 - 2 \times 33.) \\
              &= \gcd(11,22) \quad (11 = 33 - 1 \times 22.) \\
              &= \gcd(11,0) = 11. \quad (0 = 22 - 2 \times 11.)
\end{align}
Therefore, $ 5 $ steps are needed to terminate the Euclidean Algorithm for the pair $ (121,330) $.
When applying the Euclidean Algorithm, the number of steps needed till termination never exceeds $ 5 $ times the number of decimal digits in the smaller number. This fact is a consequence of Lamé’s Theorem. (Some authors label the fact itself as Lamé’s Theorem; see this link for an engaging discussion). The proof of the theorem, which can also be found in the preceding link, is interesting for its use of Fibonacci numbers.
A: You don't need any divisions at all! You only need remainders, not quotients. As for how many remainders you need:
330/121 gives remainder 88
121/88 gives remainder 33
and so on. Stop when you get a remainder of 0 (and then the answer is the last non-zero remainder). It won't take you long :-)
