find the surface area of a regular polygon inscribed in a triangular prism in go

To find the surface area of a regular polygon inscribed in a triangular prism, we need to find the area of the three sides and the area of the base.

Here's the algorithm we can use:

  1. Define the number of sides (n) of the regular polygon inscribed in the triangular prism and the length of each side (s).
  2. Calculate the height of the prism (h) using the Pythagorean theorem. The height is the square root of the difference between the length of one side of the triangle and half the length of the base of the polygon, squared.
  3. Find the perimeter of the regular polygon by multiplying the length of one side (s) by the number of sides (n).
  4. Use the formula to find the area of a regular polygon, which is A = (1/4)n * (s^2) * cot(pi/n).
  5. Multiply the area of the polygon by the height of the prism to get the surface area.

Here's the code in go:

main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    n := 6        // number of sides
    s := 5.0      // length of each side
    b := s / math.Cos(math.Pi/float64(n))
    h := math.Sqrt(s*s - (b/2)*(b/2))   // height of prism

    perimeter := float64(n) * s
    area := 0.25 * float64(n) * s * s * (1 / math.Tan(math.Pi/float64(n)))
    surfaceArea := (perimeter * h) + (2 * area)

    fmt.Println("Surface area of regular polygon inscribed in triangular prism:", surfaceArea)
}
494 chars
20 lines

Note that we use the math package to access the math functions, including square root and tangent. The output will be the surface area of the regular polygon inscribed in the triangular prism, in the appropriate units.

gistlibby LogSnag