*Method 1*

```
gcdList[x_, y_] :=
FixedPointList[
# /. {a_, b_} /; b != 0 :> {b, Mod[a, b]} &, {x, y}]
gcdList[25,45]
```

```
{{25, 45}, {45, 25}, {25, 20}, {20, 5}, {5, 0}, {5, 0}}
```

Another solution

```
gcd[a_, b_] :=
Module[{x, y},
{x, y} = {a, b};
While[y != 0,
{x, y} = {y, Mod[x, y]};
];
x
]
gcd[15,10]
```

```
5
```

*Method 2*

```
gcdList2[a_, b_] :=
Module[{x, y, res = {}},
{x, y} = {a, b};
While[y != 0,
AppendTo[res, {x, y}];
{x, y} = {y, Mod[x, y]}
];
AppendTo[res, {x, 0}]
]
gcdList2[56, 21]
```

```
{{56, 21}, {21, 14}, {14, 7}, {7, 0}}
```

*Method 3*

With the help of `Sow`

and `Reap`

```
gcdList3[a_, b_] :=
Module[{x, y, res},
{x, y} = {a, b};
res =
Reap[
While[y != 0,
Sow[{x, y}];
{x, y} = {y, Mod[x, y]}
]];
AppendTo[res[[2, 1]], {x, 0}]
]
gcdlist3[120, 75]
```

```
{{120, 75}, {75, 45}, {45, 30}, {30, 15}, {15, 0}}
```

1

You may want to look into

– MarcoB – 2015-05-22T18:58:48.880`NestWhile`

. It will allow you to apply a function recursively until a user-specified condition is met. Here is its documentation page. Also, unless you are doing this for practice,Mathematicaalready has a built-in function to calculate the greatest common divisor, called`GCD`

(docs here). You could use that one as a step towards solving your problem.Thank you for the advice, but I am still stuck, this is what I've tried:

If you are trying to implement the standard Euclid algorithm for gcd you should take a look at the recursive implementation here. Your version seems overly complicated.

– C. E. – 2015-05-22T19:53:33.210