The Euclidean Algorithm is an algorithm that computes for non-negative integers and in time and space.

Tip

This problem can also be solved by the Extended Euclidean Algorithm in time and space.

Algorithm

Lemma

  1. Solve for recursively.

  2. Apply the lemma to find .

This algorithm solves the problem in time and space.

int euclidean(int a, int b) {
	return y_combinator([&](auto &&self, int a, int b) -> int {
		return b ? self(b, a % b) : a;
	})(a, b);
}