Skip to content

Commit cc0405d

Browse files
yousha806pre-commit-ci[bot]tianyizheng02
authored
Update volume.py with volume of Icosahedron (TheAlgorithms#9628)
* Update volume.py with volume of Icosahedron Added function to find volume of a regular Icosahedron * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review * Update volume.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <[email protected]>
1 parent 1a26d76 commit cc0405d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

maths/volume.py

+30
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,35 @@ def vol_torus(torus_radius: float, tube_radius: float) -> float:
469469
return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2)
470470

471471

472+
def vol_icosahedron(tri_side: float) -> float:
473+
"""Calculate the Volume of an Icosahedron.
474+
Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
475+
476+
>>> from math import isclose
477+
>>> isclose(vol_icosahedron(2.5), 34.088984228514256)
478+
True
479+
>>> isclose(vol_icosahedron(10), 2181.694990624912374)
480+
True
481+
>>> isclose(vol_icosahedron(5), 272.711873828114047)
482+
True
483+
>>> isclose(vol_icosahedron(3.49), 92.740688412033628)
484+
True
485+
>>> vol_icosahedron(0)
486+
0.0
487+
>>> vol_icosahedron(-1)
488+
Traceback (most recent call last):
489+
...
490+
ValueError: vol_icosahedron() only accepts non-negative values
491+
>>> vol_icosahedron(-0.2)
492+
Traceback (most recent call last):
493+
...
494+
ValueError: vol_icosahedron() only accepts non-negative values
495+
"""
496+
if tri_side < 0:
497+
raise ValueError("vol_icosahedron() only accepts non-negative values")
498+
return tri_side**3 * (3 + 5**0.5) * 5 / 12
499+
500+
472501
def main():
473502
"""Print the Results of Various Volume Calculations."""
474503
print("Volumes:")
@@ -489,6 +518,7 @@ def main():
489518
print(
490519
f"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }"
491520
) # ~= 28.3
521+
print(f"Icosahedron: {vol_icosahedron(2.5) = }") # ~=34.09
492522

493523

494524
if __name__ == "__main__":

0 commit comments

Comments
 (0)