find the nth nonagonal number in rust

To find the nth nonagonal number in Rust, we can use the formula:

nonagonal_number = n * (7n - 5) / 2

where n is the index of the nonagonal number.

We can create a function that takes the index as an argument and returns the corresponding nonagonal number:

main.rs
fn get_nonagonal_number(n: i32) -> i32 {
    n * (7 * n - 5) / 2
}
67 chars
4 lines

We can then call this function with the desired index to get the nonagonal number:

main.rs
fn main() {
    let index = 4;
    let nonagonal_number = get_nonagonal_number(index);
    println!("The {}th nonagonal number is {}", index, nonagonal_number);
}
163 chars
6 lines

This will output:

main.rs
The 4th nonagonal number is 46
31 chars
2 lines

gistlibby LogSnag