find the nth decagonal number in rust

To find the nth decagonal number in Rust, we first need to understand what a decagonal number is. A decagonal number is a figurate number that represents a regular 10-sided polygon with a dot in the center and all other dots equally spaced around the perimeter. The nth decagonal number can be calculated using the formula:

n(7n-5)/2

where n is the nth term in the sequence.

To implement this formula in Rust, we can create a function that takes an integer as input and returns the corresponding nth decagonal number:

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

We can then call this function and pass in the value of n to get the corresponding nth decagonal number:

main.rs
fn main() {
    let n = 7;
    let decagonal_num = decagonal_number(n);
    println!("The {}th decagonal number is: {}", n, decagonal_num);
}
142 chars
6 lines

Output:

main.rs
The 7th decagonal number is: 133
33 chars
2 lines

gistlibby LogSnag