To find the nth heptagonal number, we can use the formula:
main.rs17 chars2 lines
where n is the index of the heptagonal number.
We can implement this formula in rust as follows:
main.rs64 chars4 lines
The heptagonal_number
function takes an unsigned 64-bit integer n
as input and returns the corresponding heptagonal number as an unsigned 64-bit integer. Note that we use u64
instead of i64
as heptagonal numbers are always positive.
We can then call this function to find the nth heptagonal number as shown below:
main.rs124 chars6 lines
This will output:
main.rs33 chars2 lines
gistlibby LogSnag