Categories

Math2

GCD

Calculates the greatest common divisor (GCD) of two numbers.

Contributed by @itsbrunodev

rust
fn gcd(mut a: u32, mut b: u32) -> u32 {
    while b != 0 {
        let temp = b;
        b = a % b;
        a = temp;
    }
    a
}
rust
gcd(10, 15); // 5
GitHubEdit on GitHub