find the volume of a regular polygon inscribed in a cylinder in go

To find the volume of a regular polygon inscribed in a cylinder in Go, we can use the following formula:

V = h * A / 3

Where:

  • V is the volume of the regular polygon
  • h is the height of the cylinder
  • A is the area of the regular polygon

To calculate the area of a regular polygon, we can use the following formula:

A = (n * s^2) / (4 * tan(π/n))

Where:

  • n is the number of sides of the polygon
  • s is the length of each side of the polygon
  • π is pi (approximately 3.141592653589793)

Here's an example function that takes the number of sides, the length of each side, and the height of the cylinder as input, and returns the volume of the regular polygon:

main.go
package main

import (
    "math"
)

func regPolygonInCylVolume(n int, s float64, h float64) float64 {
    area := float64(n) * s * s / (4 * math.Tan(math.Pi/float64(n)))
    return h * area / 3
}
197 chars
11 lines

You can call this function with the appropriate arguments to get the volume of the regular polygon inscribed in a cylinder.

gistlibby LogSnag