From 44d1069d50092fa6dbf713f1e93cb9dc25760e0e Mon Sep 17 00:00:00 2001 From: Gopesh Pandey Date: Sat, 17 Jan 2026 03:44:27 +0530 Subject: [PATCH 1/3] Added volume_of_torus algorithm in maths Implemented the formula for the volume of a torus with type hints and doctests. --- maths/volume_of_torus.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 maths/volume_of_torus.py diff --git a/maths/volume_of_torus.py b/maths/volume_of_torus.py new file mode 100644 index 000000000000..3bb97364692c --- /dev/null +++ b/maths/volume_of_torus.py @@ -0,0 +1,38 @@ +""" +Volume of a Torus +Reference: https://en.wikipedia.org/wiki/Torus +""" + +from math import pi + + +def volume_of_torus(major_radius: float, minor_radius: float) -> float: + """ + Calculate the volume of a torus. + + Formula: + V = 2 * π² * R * r² + + :param major_radius: Distance from the center of the tube to the center of the torus (R) + :param minor_radius: Radius of the tube (r) + :return: Volume of the torus + + >>> volume_of_torus(3.0, 1.0) + 59.21762640653615 + >>> volume_of_torus(5.0, 2.0) + 394.7841760435743 + >>> volume_of_torus(0.0, 0.0) + 0.0 + """ + if major_radius < 0: + raise ValueError("major_radius must be non-negative") + if minor_radius < 0: + raise ValueError("minor_radius must be non-negative") + + return 2 * (pi ** 2) * major_radius * (minor_radius ** 2) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From dada185cc64796f8e211a6ec688831e7cb679e33 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Jan 2026 22:18:35 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/volume_of_torus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/volume_of_torus.py b/maths/volume_of_torus.py index 3bb97364692c..f517ad8af1a1 100644 --- a/maths/volume_of_torus.py +++ b/maths/volume_of_torus.py @@ -29,7 +29,7 @@ def volume_of_torus(major_radius: float, minor_radius: float) -> float: if minor_radius < 0: raise ValueError("minor_radius must be non-negative") - return 2 * (pi ** 2) * major_radius * (minor_radius ** 2) + return 2 * (pi**2) * major_radius * (minor_radius**2) if __name__ == "__main__": From d2caa39bd83e263d257237fddeacf742f69e9dcb Mon Sep 17 00:00:00 2001 From: Gopesh Pandey Date: Sat, 17 Jan 2026 18:31:27 +0530 Subject: [PATCH 3/3] Fix formatting and style issues in volume_of_torus --- maths/volume_of_torus.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/maths/volume_of_torus.py b/maths/volume_of_torus.py index f517ad8af1a1..61232705551d 100644 --- a/maths/volume_of_torus.py +++ b/maths/volume_of_torus.py @@ -1,5 +1,6 @@ """ -Volume of a Torus +Volume of a Torus. + Reference: https://en.wikipedia.org/wiki/Torus """ @@ -21,15 +22,11 @@ def volume_of_torus(major_radius: float, minor_radius: float) -> float: 59.21762640653615 >>> volume_of_torus(5.0, 2.0) 394.7841760435743 - >>> volume_of_torus(0.0, 0.0) - 0.0 """ - if major_radius < 0: - raise ValueError("major_radius must be non-negative") - if minor_radius < 0: - raise ValueError("minor_radius must be non-negative") + if major_radius < 0 or minor_radius < 0: + raise ValueError("Radii must be non-negative") - return 2 * (pi**2) * major_radius * (minor_radius**2) + return 2 * pi**2 * major_radius * minor_radius**2 if __name__ == "__main__":