Skip to content
← All examples

GCD (Euclid)

Rust Algorithms

while + swap

Flowchart (ISO 5807)

YesNoStartInput a, bb != 0t = bb = a % ba = tReturn aEndFigure 1 — gcd

Source code

fn gcd(mut a: u64, mut b: u64) -> u64 {
    while b != 0 {
        let t = b;
        b = a % b;
        a = t;
    }
    a
}