find the nth nonagonal number in go

Nonagonal numbers are figurate numbers that represent the number of dots in a pattern of evenly spaced dots forming a regular nonagon.

The formula to find the nth nonagonal number is: n * (7n - 5) / 2

Here's the implementation of the formula in Go:

main.go
package main

import (
	"fmt"
)

func findNthNonagonalNumber(n int) int {
	return n * (7*n - 5) / 2
}

func main() {
	n := 5
	nthNonagonalNumber := findNthNonagonalNumber(n)
	fmt.Printf("%dth Nonagonal Number: %d\n", n, nthNonagonalNumber)
}
242 chars
16 lines

This will output:

main.go
5th Nonagonal Number: 70
25 chars
2 lines

You can change the value of n to find the nth nonagonal number you want.

related categories

gistlibby LogSnag