Skip to content

Commit 098a1b2

Browse files
authored
Create volume_of_torus.py
1 parent 3c88735 commit 098a1b2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

maths/volume_of_torus.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Volume of a Torus
3+
Board: https://en.wikipedia.org/wiki/Torus
4+
"""
5+
import math
6+
7+
def volume_of_torus(major_radius: float, minor_radius: float) -> float:
8+
"""
9+
Calculate the volume of a torus.
10+
11+
:param major_radius: Distance from the center of the tube to the center of the torus (R)
12+
:param minor_radius: Radius of the tube (r)
13+
:return: Volume of the torus
14+
15+
>>> volume_of_torus(3, 1)
16+
59.21762640653615
17+
>>> volume_of_torus(5, 2)
18+
394.7841760435743
19+
"""
20+
if major_radius < 0 or minor_radius < 0:
21+
raise ValueError("Radii must be non-negative")
22+
return 2 * (math.pi ** 2) * major_radius * (minor_radius ** 2)
23+
24+
if __name__ == "__main__":
25+
import doctest
26+
doctest.testmod()

0 commit comments

Comments
 (0)