find the nth decagonal number in go

To find the nth decagonal number, we can use the formula:

dn = 3n^2 - 2n

where n is the index of the decagonal number.

We can create a function in Go to return the nth decagonal number using this formula:

main.go
func decagonalNumber(n int) int {
    return 3*n*n - 2*n
}
59 chars
4 lines

We can then call this function with the desired value of n to find the corresponding decagonal number:

main.go
fmt.Println(decagonalNumber(5)) // Output: 133
47 chars
2 lines

This will output 133, which is the 5th decagonal number.

gistlibby LogSnag