Greatest Common Denominator

Uses the Euclidean Algorithm to get Greatest Common Denominator

use std::str::FromStr;
use std::env;

fn gcd(mut n: u64, mut m: u64) -> u64 {
    // Make sure that either number isn't 0
    assert!(n != 0 && m != 0);

    // Run a loop while the remainder `m` is not 0
    while m != 0 {
        // Make sure we are always trying to get the divsor of
        // the smaller number
        if m < n {
            // initialize a temp variable
            let temp = m;
            m = n;
            n = temp;
        }
        // Get the divisor
        m = m % n;
    }
    // return n aka the greatest common denominator
    n
}

#[test]
fn test_gcd() {
    assert_eq!(gcd(14, 15), 1);

    assert_eq!(gcd(8, 4), 4);
}

fn main() {
    // Initiate the vector to put in you CLI args
    let mut numbers = vec![];

    // Parse the CLI args
    // The first arg is aleays the name of the program so skip it
    for arg in env::args().skip(1) {
        numbers.push(u64::from_str(&arg)
                    // Parse the arg and throw an error iff there is an issue
                    .expect("error parsing argument"));
    }

    // Throw error and exit if there are no args
    if numbers.len() == 0 {
        eprintln!("Usage: gcd NUMBER ...");
        std::process::exit(1);
    }

    // Iterate through the args
    let mut d = numbers[0];
    for m in &numbers[1..] {
        d = gcd(d, *m);
    }

    // Print the gcd
    println!("The greatest common divisor of {:?} is {}",
            numbers, d);
}