Worldscope

Write a Program to find the Area of Hexagonal Prism

Palavras-chave:

Publicado em: 05/08/2025

Calculating the Surface Area of a Hexagonal Prism

This article explains how to write a program to calculate the total surface area of a hexagonal prism. A hexagonal prism is a 3D geometric shape with two hexagonal bases and six rectangular sides. We will provide a code example in Python, explain the logic, and discuss the complexity of the solution.

Fundamental Concepts / Prerequisites

Before diving into the code, it's essential to understand the following:

  • Hexagon Area: The area of a regular hexagon with side length 'a' is given by the formula: (3 * sqrt(3) * a^2) / 2
  • Rectangle Area: The area of a rectangle is calculated as width * height. In our case, the width is the side length of the hexagon ('a'), and the height is the height of the prism ('h').
  • Surface Area of a Prism: The total surface area of a prism is the sum of the areas of its bases and lateral faces. For a hexagonal prism, this means two hexagon areas plus six rectangle areas.

Core Implementation/Solution

Here's a Python function to calculate the surface area of a hexagonal prism:


import math

def hexagonal_prism_surface_area(side_length, height):
  """
  Calculates the total surface area of a hexagonal prism.

  Args:
    side_length: The length of one side of the hexagonal base.
    height: The height of the prism.

  Returns:
    The total surface area of the hexagonal prism.
  """

  hexagon_area = (3 * math.sqrt(3) * side_length**2) / 2
  rectangle_area = side_length * height
  total_surface_area = 2 * hexagon_area + 6 * rectangle_area

  return total_surface_area

# Example usage:
side = 5
height = 10
area = hexagonal_prism_surface_area(side, height)
print(f"The surface area of a hexagonal prism with side length {side} and height {height} is: {area}")

Code Explanation

Let's break down the Python code:

First, we import the math module to use the sqrt function for calculating the square root.

The hexagonal_prism_surface_area function takes two arguments: side_length (the length of a side of the hexagon) and height (the height of the prism).

Inside the function:

  • hexagon_area is calculated using the formula (3 * math.sqrt(3) * side_length**2) / 2.
  • rectangle_area is calculated as side_length * height.
  • total_surface_area is calculated as 2 * hexagon_area + 6 * rectangle_area, accounting for the two hexagonal bases and six rectangular sides.

Finally, the function returns the total_surface_area.

The example usage demonstrates how to call the function with sample values and print the result, utilizing an f-string for formatted output.

Complexity Analysis

Here's an analysis of the time and space complexity of the solution:

  • Time Complexity: The time complexity is O(1) because the calculations involve a fixed number of arithmetic operations regardless of the input size.
  • Space Complexity: The space complexity is O(1) because the function uses a constant amount of extra space to store variables like hexagon_area, rectangle_area, and total_surface_area. The space required doesn't grow with the input values.

Alternative Approaches

Another approach would be to calculate the hexagon area using trigonometry, by dividing the hexagon into six equilateral triangles. However, this would still result in a O(1) time complexity, and would potentially be less efficient in terms of code readability, though theoretically could provide the same computational performance depending on the specific hardware's optimized math libraries.

Conclusion

This article demonstrated how to calculate the surface area of a hexagonal prism using a Python program. The program utilizes the formulas for the area of a hexagon and a rectangle, and its time and space complexity are both O(1), making it an efficient solution. Understanding the geometric principles and translating them into code is the key to solving such problems effectively.