mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Update volume.py with volume of Icosahedron (#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 <tianyizheng02@gmail.com>
This commit is contained in:
parent
1a26d76c60
commit
cc0405d05c
|
@ -469,6 +469,35 @@ def vol_torus(torus_radius: float, tube_radius: float) -> float:
|
||||||
return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2)
|
return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def vol_icosahedron(tri_side: float) -> float:
|
||||||
|
"""Calculate the Volume of an Icosahedron.
|
||||||
|
Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
|
||||||
|
|
||||||
|
>>> from math import isclose
|
||||||
|
>>> isclose(vol_icosahedron(2.5), 34.088984228514256)
|
||||||
|
True
|
||||||
|
>>> isclose(vol_icosahedron(10), 2181.694990624912374)
|
||||||
|
True
|
||||||
|
>>> isclose(vol_icosahedron(5), 272.711873828114047)
|
||||||
|
True
|
||||||
|
>>> isclose(vol_icosahedron(3.49), 92.740688412033628)
|
||||||
|
True
|
||||||
|
>>> vol_icosahedron(0)
|
||||||
|
0.0
|
||||||
|
>>> vol_icosahedron(-1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: vol_icosahedron() only accepts non-negative values
|
||||||
|
>>> vol_icosahedron(-0.2)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: vol_icosahedron() only accepts non-negative values
|
||||||
|
"""
|
||||||
|
if tri_side < 0:
|
||||||
|
raise ValueError("vol_icosahedron() only accepts non-negative values")
|
||||||
|
return tri_side**3 * (3 + 5**0.5) * 5 / 12
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Print the Results of Various Volume Calculations."""
|
"""Print the Results of Various Volume Calculations."""
|
||||||
print("Volumes:")
|
print("Volumes:")
|
||||||
|
@ -489,6 +518,7 @@ def main():
|
||||||
print(
|
print(
|
||||||
f"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }"
|
f"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }"
|
||||||
) # ~= 28.3
|
) # ~= 28.3
|
||||||
|
print(f"Icosahedron: {vol_icosahedron(2.5) = }") # ~=34.09
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user