find the least common multiple of two numbers in rust

To find the least common multiple (LCM) of two numbers in Rust, we can use the "gcd" function (greatest common divisor) provided by the crate "num". We can then use the formula:

LCM = (number1 * number2) / gcd(number1, number2)

Here's an example implementation:

main.rs
use num::integer::gcd; // import gcd function from the num crate

fn lcm(number1: i32, number2: i32) -> i32 {
    (number1 * number2) / gcd(number1, number2)
}

fn main() {
    let number1 = 12;
    let number2 = 24;
    let lcm_result = lcm(number1, number2);

    println!("LCM of {} and {} is {}", number1, number2, lcm_result);
}
334 chars
14 lines

In this example, we import the gcd function from the num crate and define our own lcm function that takes in two integers and returns their LCM. We then use the lcm function in the main function to find the LCM of 12 and 24.

When we run this program, we should see the output:

LCM of 12 and 24 is 24

gistlibby LogSnag