As you are still learning and mentioning the ability to transform from recursive to iterative, I took the liberty of implementing all three options that come in mind for now. The names of the methods are intuitive enough, but better more information than less:
gcdStream - uses the implementation with streams. With some inspiration from here
gcdRecursive - is the classic algorithm based on recursion, the one used by you as well
gcdIterative - is the transformation of the recursive algorithm into an iterative one (typically you transform them using while loops)
gcdStream - implementation using streams. Imo is the closest transformation of the classic algorithm into one based on streams. Nice that it uses for iteration the modulo operator, so it should have less iterations then going linearly
gcdStreamPair - though each one functional, I feel the gcdStream a bit verbose. So, I looked into various options to make it more readable. Replacing the array with an immutable Pair class makes it a bit more readable. See the implementation in the code sample.
Looking into the methods in this way gcdRecursive -> gcdIterative -> gcdStream the translation should feel natural.
I placed all the methods in an UT, typically you could use it straight away.
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
public class GcdStreams {
@Test
public void t() {
int a = 72, b = 48, expectedGcd = 24;
assertEquals(expectedGcd, gcdStream(a,b));
}
private int gcdRecursive(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
private int gcdIterative(int a, int b) {
do {
int first = a;
a = b;
b = first % b;
} while (b != 0);
return a;
}
private int gcdStream(int a, int b) {
return Stream.iterate(new int[] {a, b}, n -> new int[]{n[1], n[0] % n[1]})
.filter(x -> x[1] == 0)
.findFirst()
.map(x -> x[0])
.get();
}
private int gcdStreamPair(int a, int b) {
return Stream.iterate(new Pair(a, b) , n -> new Pair(n.b, n.a % n.b))
.filter(x -> x.b == 0)
.findFirst()
.map(x -> x.a)
.get();
}
private class Pair {
final int a,b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
}