Skip to content

Commit 6b2abab

Browse files
authored
Update area.py
Added a function to calculate the surface area of a regular prism
1 parent c9a3ddd commit 6b2abab

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

maths/area.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,50 @@ def area_reg_polygon(sides: int, length: float) -> float:
607607
return (sides * length**2) / (4 * tan(pi / sides))
608608

609609

610+
def surface_area_reg_prism(sides: int, edge: float, height: float) -> float:
611+
"""
612+
Calculate the surface area of a regular prism.
613+
Wikipedia reference: https://en.wikipedia.org/wiki/Prism
614+
Formula: 2*S(base) + n*a*h
615+
616+
>>> surface_area_reg_prism(3, 10, 20)
617+
686.6025403784439
618+
>>> surface_area_reg_prism(4, 10, 10)
619+
600.0
620+
>>> surface_area_reg_prism(2, 10, 15)
621+
Traceback (most recent call last):
622+
...
623+
ValueError: surface_area_reg_prism() only accepts integers greater than or equal to \
624+
three as number of sides
625+
>>> surface_area_reg_prism(5, -2, 3)
626+
Traceback (most recent call last):
627+
...
628+
ValueError: surface_area_reg_prism() only accepts non-negative values as \
629+
length of an edge or height
630+
>>> surface_area_reg_prism(5, 2, -3)
631+
Traceback (most recent call last):
632+
...
633+
ValueError: surface_area_reg_prism() only accepts non-negative values as \
634+
length of an edge or height
635+
>>> surface_area_reg_prism(2, -10, 15)
636+
Traceback (most recent call last):
637+
...
638+
ValueError: surface_area_reg_prism() only accepts integers greater than or equal to \
639+
three as number of sides
640+
"""
641+
if not isinstance(sides, int) or sides < 3:
642+
raise ValueError(
643+
"surface_area_reg_prism() only accepts integers greater than or \
644+
equal to three as number of sides"
645+
)
646+
elif edge < 0 or height < 0:
647+
raise ValueError(
648+
"surface_area_reg_prism() only accepts non-negative values as \
649+
length of an edge or height"
650+
)
651+
return 2 * area_reg_polygon(sides, edge) + sides * edge * height
652+
653+
610654
if __name__ == "__main__":
611655
import doctest
612656

@@ -636,3 +680,5 @@ def area_reg_polygon(sides: int, length: float) -> float:
636680
print(f"Equilateral Triangle: {area_reg_polygon(3, 10) = }")
637681
print(f"Square: {area_reg_polygon(4, 10) = }")
638682
print(f"Reqular Pentagon: {area_reg_polygon(5, 10) = }")
683+
print(f"Regular Pentagonal Prism: {surface_area_reg_prism(5, 10, 15) = }")
684+

0 commit comments

Comments
 (0)