To find the nth pentagonal number we can use the formula: Pn = n(3n-1)/2
We can define a function to implement this formula as follows:
main.rs57 chars4 linesThis function takes an argument n of type u32 and returns the nth pentagonal number as output.
We can test our function by calling it with different values of n as follows:
main.rs293 chars12 linesThis will output:
main.rs66 chars3 lines
Note that since we are using integer arithmetic, our function will return the pentagonal number as an integer. If we need to find pentagonal numbers with higher values of n we should use a larger integer type like u64.
gistlibby LogSnag