find the nth hexagonal number in rust

To find the nth hexagonal number, we will use the formula:

main.rs
h_n = 2n^2 - n
15 chars
2 lines

where h_n represents the nth hexagonal number.

Here's the implementation of this formula in Rust:

main.rs
fn hexagonal(n: i32) -> i32 {
    2 * n * n - n
}
50 chars
4 lines

You can simply call this function by passing the value of n and it will return the corresponding hexagonal number.

For example, to find the 6th hexagonal number, you can call the function like this:

main.rs
let n = 6;
let hexagonal_number = hexagonal(n);
println!("{}", hexagonal_number); // Output: 61
96 chars
4 lines

So, the 6th hexagonal number is 61.

gistlibby LogSnag