Iterative greatest common divisor

Write an iterative function that computes the greatest common divisor of two natural numbers @a@ and @b@ using the fast version of the Euclidean algorithm.

Interface

C++
int gcd(int a, int b);
C
int gcd(int a, int b);
Java
public static int gcd(int a, int b);
Python
gcd(a, b)  # returns int
gcd(a: int, b: int) -> int

Precondition

Neither @a@ nor @b@ are negative, and at least one of them is strictly greater than zero.

Observation

You only need to submit the required procedure; your main program will be ignored.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T12:17:02.553Z

© Jutge.org, 2006–2026.
https://jutge.org