A generalization of the Euclidean algorithm to several dimensions