From f1a39f37def4b9b0a59cd8289a62ff62472cc96f Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 13:06:01 +0530 Subject: [PATCH 01/18] Added iterative solution for power calculation --- maths/power_using_iteration.py | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 maths/power_using_iteration.py diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py new file mode 100644 index 000000000000..023fe5f32a6f --- /dev/null +++ b/maths/power_using_iteration.py @@ -0,0 +1,85 @@ +""" +Iterative solution to calculate the power of a base raised to an exponent. + +This implementation uses an iterative approach, unlike the recursive approach +in `power_using_recursion.py`. The algorithm is based on exponentiation by squaring +for optimal performance. + +Examples: + >>> power(2, 3) + 8 + >>> power(5, -2) + 0.04 + >>> power(10, 0) + 1 + >>> Failed example: + power(5, -2) + Expected: + 0.04 + Got: + 0.04000000000000001 + 1 items had failures: + 1 of 3 in __main__ + 1 failed in total + Raise base to the power of exponent using an optimized approach... + Enter the base: 500 + Enter the exponent: 8 + 500.0 to the power of 8 is 3.90625e+21 + +Input: + base (float): The base number (can be integer or float). + exponent (int): The exponent (can be positive, negative, or zero). + +Output: + float: The result of base raised to the power of exponent. + +Note: + Results for very large or very small floating-point numbers may have slight precision errors + due to the limitations of floating-point arithmetic. +""" + + +def power(base: float, exponent: int) -> float: + """ + Optimized power function using exponentiation by squaring. + It handles both positive and negative exponents efficiently. + This function take time complexity O(log n) for exponentiation. + space complexity is O(1) as it uses a constant amount of space. + """ + # Handle negative exponents by taking reciprocal of the base + if exponent < 0: + base = 1 / base + exponent = -exponent + + result = 1 + # Use exponentiation by squaring for efficiency + while exponent: + if exponent % 2 == 1: # If the current exponent is odd + result *= base + base *= base # Square the base + exponent //= 2 # Halve the exponent + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print("Raise base to the power of exponent using an optimized approach...") + + try: + # Input handling and validation + base = float(input("Enter the base: ").strip()) # Supports float & int + exponent = int( + input("Enter the exponent: ").strip() + ) # Ensures exponent is an integer + + # Calculate result + result = power(base, exponent) + + # Display the result + print(f"{base} to the power of {exponent} is {result}") + + except ValueError: + # Handle invalid input + print("Invalid input! Please enter numeric values for base and exponent.") From 89ad00e4433968d40bd6129c9d7dddb1c775ce9b Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 13:20:38 +0530 Subject: [PATCH 02/18] Added iterative solution for power calculation --- maths/power_using_iteration.py | 43 +--------------------------------- 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index 023fe5f32a6f..cd92b9a324a1 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -1,44 +1,3 @@ -""" -Iterative solution to calculate the power of a base raised to an exponent. - -This implementation uses an iterative approach, unlike the recursive approach -in `power_using_recursion.py`. The algorithm is based on exponentiation by squaring -for optimal performance. - -Examples: - >>> power(2, 3) - 8 - >>> power(5, -2) - 0.04 - >>> power(10, 0) - 1 - >>> Failed example: - power(5, -2) - Expected: - 0.04 - Got: - 0.04000000000000001 - 1 items had failures: - 1 of 3 in __main__ - 1 failed in total - Raise base to the power of exponent using an optimized approach... - Enter the base: 500 - Enter the exponent: 8 - 500.0 to the power of 8 is 3.90625e+21 - -Input: - base (float): The base number (can be integer or float). - exponent (int): The exponent (can be positive, negative, or zero). - -Output: - float: The result of base raised to the power of exponent. - -Note: - Results for very large or very small floating-point numbers may have slight precision errors - due to the limitations of floating-point arithmetic. -""" - - def power(base: float, exponent: int) -> float: """ Optimized power function using exponentiation by squaring. @@ -58,7 +17,7 @@ def power(base: float, exponent: int) -> float: result *= base base *= base # Square the base exponent //= 2 # Halve the exponent - return result + return round(result, 5) # Round to 5 decimal places if __name__ == "__main__": From ee3f4be0b848b4a431d549902024ece53fb082f0 Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 15:43:59 +0530 Subject: [PATCH 03/18] Added iterative solution for power calculation --- maths/power_using_iteration.py | 47 +++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index cd92b9a324a1..916a8ecbf9ba 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -1,19 +1,49 @@ def power(base: float, exponent: int) -> float: """ Optimized power function using exponentiation by squaring. - It handles both positive and negative exponents efficiently. - This function take time complexity O(log n) for exponentiation. - space complexity is O(1) as it uses a constant amount of space. + + Args: + base (float): The base number. + exponent (int): The exponent. + + Returns: + float: The result of base raised to the power of exponent. + + Examples: + >>> power(2, 3) + 8.0 + >>> power(5, -2) + 0.04 + >>> power(10, 0) + 1.0 + >>> power(7, 2) + 49.0 + >>> power(2, -3) + 0.125 + >>> power(2.5, 4) + 39.0625 + >>> power(-3.5, 2) + 12.25 + >>> power(-2, 3) + -8.0 + >>> power(0, 5) + 0.0 + >>> power(0, 0) + 1.0 + >>> power(0, -1) + Traceback (most recent call last): + ... + ZeroDivisionError: 0.0 cannot be raised to a negative power. + >>> power(1, 1000) + 1.0 + """ - # Handle negative exponents by taking reciprocal of the base + result = 1.0 if exponent < 0: base = 1 / base exponent = -exponent - - result = 1 - # Use exponentiation by squaring for efficiency while exponent: - if exponent % 2 == 1: # If the current exponent is odd + if exponent % 2 == 1: result *= base base *= base # Square the base exponent //= 2 # Halve the exponent @@ -22,7 +52,6 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": import doctest - doctest.testmod() print("Raise base to the power of exponent using an optimized approach...") From a55058b80b94508234336693141d73a23006b3d9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 10:14:28 +0000 Subject: [PATCH 04/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/power_using_iteration.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index 916a8ecbf9ba..322771b86830 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -24,7 +24,7 @@ def power(base: float, exponent: int) -> float: 39.0625 >>> power(-3.5, 2) 12.25 - >>> power(-2, 3) + >>> power(-2, 3) -8.0 >>> power(0, 5) 0.0 @@ -34,9 +34,9 @@ def power(base: float, exponent: int) -> float: Traceback (most recent call last): ... ZeroDivisionError: 0.0 cannot be raised to a negative power. - >>> power(1, 1000) + >>> power(1, 1000) 1.0 - + """ result = 1.0 if exponent < 0: @@ -52,6 +52,7 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": import doctest + doctest.testmod() print("Raise base to the power of exponent using an optimized approach...") From 1b86189d40c46ce8ac649df1259e959f4ae3de2b Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 15:54:29 +0530 Subject: [PATCH 05/18] Added iterative solution for power calculation fixes #12709 --- maths/power_using_iteration.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index 916a8ecbf9ba..bd79fa515ab6 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -28,16 +28,25 @@ def power(base: float, exponent: int) -> float: -8.0 >>> power(0, 5) 0.0 - >>> power(0, 0) - 1.0 + >>> power(0, 1) + 0.0 >>> power(0, -1) Traceback (most recent call last): ... ZeroDivisionError: 0.0 cannot be raised to a negative power. + >>> power(0, 0) + Traceback (most recent call last): + ... + ValueError: 0.0 raised to the power of 0 is indeterminate. >>> power(1, 1000) 1.0 """ + if base == 0 and exponent == 0: + raise ValueError("0.0 raised to the power of 0 is indeterminate.") + if base == 0 and exponent < 0: + raise ZeroDivisionError("0.0 cannot be raised to a negative power.") + result = 1.0 if exponent < 0: base = 1 / base @@ -51,6 +60,7 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": + import doctest doctest.testmod() print("Raise base to the power of exponent using an optimized approach...") @@ -68,6 +78,9 @@ def power(base: float, exponent: int) -> float: # Display the result print(f"{base} to the power of {exponent} is {result}") - except ValueError: - # Handle invalid input - print("Invalid input! Please enter numeric values for base and exponent.") + except ValueError as e: + # Handle invalid input or indeterminate cases + print(e) + except ZeroDivisionError as e: + # Handle division by zero + print(e) From bde13935f18372f64197a23d7e3257c5b09d2689 Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 16:01:31 +0530 Subject: [PATCH 06/18] Added iterative solution for power calculation FIXES NUMBER 12709 --- maths/power_using_recursion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py index eb775b161ae8..c560503525a3 100644 --- a/maths/power_using_recursion.py +++ b/maths/power_using_recursion.py @@ -60,4 +60,4 @@ def power(base: int, exponent: int) -> float: result = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents result = 1 / result - print(f"{base} to the power of {exponent} is {result}") + print(f"{base} to the power of {exponent} is {result}") \ No newline at end of file From 935febedb187032e8bd8cc79be0a861acf100d30 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 10:40:32 +0000 Subject: [PATCH 07/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/power_using_iteration.py | 3 +-- maths/power_using_recursion.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index c14208af4b3f..f86b6454f4c3 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -46,7 +46,7 @@ def power(base: float, exponent: int) -> float: raise ValueError("0.0 raised to the power of 0 is indeterminate.") if base == 0 and exponent < 0: raise ZeroDivisionError("0.0 cannot be raised to a negative power.") - + result = 1.0 if exponent < 0: base = 1 / base @@ -60,7 +60,6 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": - import doctest doctest.testmod() diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py index c560503525a3..eb775b161ae8 100644 --- a/maths/power_using_recursion.py +++ b/maths/power_using_recursion.py @@ -60,4 +60,4 @@ def power(base: int, exponent: int) -> float: result = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents result = 1 / result - print(f"{base} to the power of {exponent} is {result}") \ No newline at end of file + print(f"{base} to the power of {exponent} is {result}") From a908d036dfecb38924e11a768578ea5ea513394a Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Sat, 10 May 2025 19:34:38 +0530 Subject: [PATCH 08/18] Escape velocity is the minimum speed an object must have to break free from a celestial body's gravitational pull without further propulsion. Takes input as the Mass of the Celestial body (M) and Radius fron the center of mass (M) --- physics/escape_velocity.py | 65 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 physics/escape_velocity.py diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py new file mode 100644 index 000000000000..83784d9b42d1 --- /dev/null +++ b/physics/escape_velocity.py @@ -0,0 +1,65 @@ +import math + +def escape_velocity(mass: float, radius: float) -> float: + """ + Calculates the escape velocity needed to break free from a celestial body's gravitational field. + + The formula used is: + v = sqrt(2 * G * M / R) + where: + v = escape velocity (m/s) + G = gravitational constant (6.67430 × 10^-11 m^3 kg^-1 s^-2) + M = mass of the celestial body (kg) + R = radius from the center of mass (m) + + Args: + mass (float): Mass of the celestial body in kilograms. + radius (float): Radius from the center of mass in meters. + + Returns: + float: Escape velocity in meters per second, rounded to 3 decimal places. + + Examples: + >>> escape_velocity(5.972e24, 6.371e6) # Earth + 11185.978 + >>> escape_velocity(7.348e22, 1.737e6) # Moon + 2376.307 + >>> escape_velocity(1.898e27, 6.9911e7) # Jupiter + 60199.545 + >>> escape_velocity(0, 1.0) + 0.0 + >>> escape_velocity(1.0, 0) + Traceback (most recent call last): + ... + ZeroDivisionError: Radius cannot be zero. + """ + G = 6.67430e-11 # Gravitational constant in m^3 kg^-1 s^-2 + + if radius == 0: + raise ZeroDivisionError("Radius cannot be zero.") + if mass == 0: + return 0.0 + + velocity = math.sqrt(2 * G * mass / radius) + return round(velocity, 3) + + +if __name__ == "__main__": + + import doctest + doctest.testmod() + print("Calculate escape velocity of a celestial body...\n") + + try: + # User input + mass = float(input("Enter mass of the celestial body (in kg): ").strip()) + radius = float(input("Enter radius from center (in meters): ").strip()) + + # Result + velocity = escape_velocity(mass, radius) + print(f"Escape velocity is {velocity} m/s") + + except ValueError: + print("Invalid input. Please enter valid numeric values.") + except ZeroDivisionError as e: + print(e) From f33964ab8f2bb0e93b8650862ae766f3ec31868c Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Sat, 10 May 2025 19:44:52 +0530 Subject: [PATCH 09/18] Fix: added header comment to escape_velocity.py --- physics/escape_velocity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index 83784d9b42d1..74ebc2883382 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -1,5 +1,5 @@ import math - +# Escape velocity calculator script def escape_velocity(mass: float, radius: float) -> float: """ Calculates the escape velocity needed to break free from a celestial body's gravitational field. From c6cfddccf45db9af45f0844d88b1887110c6b240 Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Sat, 10 May 2025 19:51:40 +0530 Subject: [PATCH 10/18] Trigger re-PR with a minor change --- physics/escape_velocity.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index 74ebc2883382..5922e54e7f10 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -1,5 +1,6 @@ import math # Escape velocity calculator script +# Minor change to trigger pull request again def escape_velocity(mass: float, radius: float) -> float: """ Calculates the escape velocity needed to break free from a celestial body's gravitational field. From 5854d54894d2a1361137219f24fac90b56ea651d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 May 2025 14:36:49 +0000 Subject: [PATCH 11/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/escape_velocity.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index 5922e54e7f10..ba67902d9d83 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -1,4 +1,6 @@ import math + + # Escape velocity calculator script # Minor change to trigger pull request again def escape_velocity(mass: float, radius: float) -> float: @@ -46,8 +48,8 @@ def escape_velocity(mass: float, radius: float) -> float: if __name__ == "__main__": - import doctest + doctest.testmod() print("Calculate escape velocity of a celestial body...\n") From fa2597a71d85498c64ceb367260567650aa83152 Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Sat, 10 May 2025 20:12:45 +0530 Subject: [PATCH 12/18] Fix: resolve Ruff linter errors and add Wikipedia reference --- physics/escape_velocity.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index ba67902d9d83..d3b166333a4f 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -1,20 +1,23 @@ import math -# Escape velocity calculator script -# Minor change to trigger pull request again def escape_velocity(mass: float, radius: float) -> float: """ - Calculates the escape velocity needed to break free from a celestial body's gravitational field. + Calculates the escape velocity needed to break free from a celestial body's + gravitational field. The formula used is: v = sqrt(2 * G * M / R) + where: v = escape velocity (m/s) - G = gravitational constant (6.67430 × 10^-11 m^3 kg^-1 s^-2) + G = gravitational constant (6.67430 * 10^-11 m^3 kg^-1 s^-2) M = mass of the celestial body (kg) R = radius from the center of mass (m) + Source: + https://en.wikipedia.org/wiki/Escape_velocity + Args: mass (float): Mass of the celestial body in kilograms. radius (float): Radius from the center of mass in meters. @@ -36,14 +39,14 @@ def escape_velocity(mass: float, radius: float) -> float: ... ZeroDivisionError: Radius cannot be zero. """ - G = 6.67430e-11 # Gravitational constant in m^3 kg^-1 s^-2 + gravitational_constant = 6.67430e-11 # m^3 kg^-1 s^-2 if radius == 0: raise ZeroDivisionError("Radius cannot be zero.") if mass == 0: return 0.0 - velocity = math.sqrt(2 * G * mass / radius) + velocity = math.sqrt(2 * gravitational_constant * mass / radius) return round(velocity, 3) @@ -54,11 +57,9 @@ def escape_velocity(mass: float, radius: float) -> float: print("Calculate escape velocity of a celestial body...\n") try: - # User input mass = float(input("Enter mass of the celestial body (in kg): ").strip()) radius = float(input("Enter radius from center (in meters): ").strip()) - # Result velocity = escape_velocity(mass, radius) print(f"Escape velocity is {velocity} m/s") From f4ea96200f28eedfe74b44cc8ec05e9a9c9f1ad6 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 10 May 2025 23:24:53 +0300 Subject: [PATCH 13/18] Delete maths/power_using_iteration.py --- maths/power_using_iteration.py | 86 ---------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 maths/power_using_iteration.py diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py deleted file mode 100644 index f86b6454f4c3..000000000000 --- a/maths/power_using_iteration.py +++ /dev/null @@ -1,86 +0,0 @@ -def power(base: float, exponent: int) -> float: - """ - Optimized power function using exponentiation by squaring. - - Args: - base (float): The base number. - exponent (int): The exponent. - - Returns: - float: The result of base raised to the power of exponent. - - Examples: - >>> power(2, 3) - 8.0 - >>> power(5, -2) - 0.04 - >>> power(10, 0) - 1.0 - >>> power(7, 2) - 49.0 - >>> power(2, -3) - 0.125 - >>> power(2.5, 4) - 39.0625 - >>> power(-3.5, 2) - 12.25 - >>> power(-2, 3) - -8.0 - >>> power(0, 5) - 0.0 - >>> power(0, 1) - 0.0 - >>> power(0, -1) - Traceback (most recent call last): - ... - ZeroDivisionError: 0.0 cannot be raised to a negative power. - >>> power(0, 0) - Traceback (most recent call last): - ... - ValueError: 0.0 raised to the power of 0 is indeterminate. - >>> power(1, 1000) - 1.0 - - """ - if base == 0 and exponent == 0: - raise ValueError("0.0 raised to the power of 0 is indeterminate.") - if base == 0 and exponent < 0: - raise ZeroDivisionError("0.0 cannot be raised to a negative power.") - - result = 1.0 - if exponent < 0: - base = 1 / base - exponent = -exponent - while exponent: - if exponent % 2 == 1: - result *= base - base *= base # Square the base - exponent //= 2 # Halve the exponent - return round(result, 5) # Round to 5 decimal places - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - print("Raise base to the power of exponent using an optimized approach...") - - try: - # Input handling and validation - base = float(input("Enter the base: ").strip()) # Supports float & int - exponent = int( - input("Enter the exponent: ").strip() - ) # Ensures exponent is an integer - - # Calculate result - result = power(base, exponent) - - # Display the result - print(f"{base} to the power of {exponent} is {result}") - - except ValueError as e: - # Handle invalid input or indeterminate cases - print(e) - except ZeroDivisionError as e: - # Handle division by zero - print(e) From a01af963defb48aa36fcb1ffb47edf97a7bb7189 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 10 May 2025 23:26:36 +0300 Subject: [PATCH 14/18] Test doctests --- physics/escape_velocity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index d3b166333a4f..c762764c4801 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -27,7 +27,7 @@ def escape_velocity(mass: float, radius: float) -> float: Examples: >>> escape_velocity(5.972e24, 6.371e6) # Earth - 11185.978 + 11185 >>> escape_velocity(7.348e22, 1.737e6) # Moon 2376.307 >>> escape_velocity(1.898e27, 6.9911e7) # Jupiter From cb9c0271ab89013a58b8154b2db6da66a90a4877 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 10 May 2025 23:29:39 +0300 Subject: [PATCH 15/18] Update escape_velocity.py --- physics/escape_velocity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index c762764c4801..d3b166333a4f 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -27,7 +27,7 @@ def escape_velocity(mass: float, radius: float) -> float: Examples: >>> escape_velocity(5.972e24, 6.371e6) # Earth - 11185 + 11185.978 >>> escape_velocity(7.348e22, 1.737e6) # Moon 2376.307 >>> escape_velocity(1.898e27, 6.9911e7) # Jupiter From ad97a08ea84b33547d63d46d90991aad915c93d0 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 10 May 2025 23:36:03 +0300 Subject: [PATCH 16/18] Update escape_velocity.py --- physics/escape_velocity.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index d3b166333a4f..31caf6410812 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -26,27 +26,25 @@ def escape_velocity(mass: float, radius: float) -> float: float: Escape velocity in meters per second, rounded to 3 decimal places. Examples: - >>> escape_velocity(5.972e24, 6.371e6) # Earth + >>> escape_velocity(mass=5.972e24, radius=6.371e6) # Earth 11185.978 - >>> escape_velocity(7.348e22, 1.737e6) # Moon + >>> escape_velocity(mass=7.348e22, radius=1.737e6) # Moon 2376.307 - >>> escape_velocity(1.898e27, 6.9911e7) # Jupiter + >>> escape_velocity(mass=1.898e27, radius=6.9911e7) # Jupiter 60199.545 - >>> escape_velocity(0, 1.0) + >>> escape_velocity(mass=0, radius=1.0) 0.0 - >>> escape_velocity(1.0, 0) + >>> escape_velocity(mass=1.0, radius=0) Traceback (most recent call last): ... ZeroDivisionError: Radius cannot be zero. """ - gravitational_constant = 6.67430e-11 # m^3 kg^-1 s^-2 + GRAVITATIONAL_CONSTANT = 6.67430e-11 # m^3 kg^-1 s^-2 if radius == 0: raise ZeroDivisionError("Radius cannot be zero.") - if mass == 0: - return 0.0 - velocity = math.sqrt(2 * gravitational_constant * mass / radius) + velocity = math.sqrt(2 * GRAVITATIONAL_CONSTANT * mass / radius) return round(velocity, 3) @@ -57,10 +55,10 @@ def escape_velocity(mass: float, radius: float) -> float: print("Calculate escape velocity of a celestial body...\n") try: - mass = float(input("Enter mass of the celestial body (in kg): ").strip()) + mass = float(input("Enter mass of the celestial body (in kgs): ").strip()) radius = float(input("Enter radius from center (in meters): ").strip()) - velocity = escape_velocity(mass, radius) + velocity = escape_velocity(mass=mass, radius=radius) print(f"Escape velocity is {velocity} m/s") except ValueError: From 1682ad4e76ed424c74eddbe3bb99df9530ca3981 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 10 May 2025 23:39:16 +0300 Subject: [PATCH 17/18] Update escape_velocity.py --- physics/escape_velocity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index 31caf6410812..4cc7c5290dd3 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -39,12 +39,12 @@ def escape_velocity(mass: float, radius: float) -> float: ... ZeroDivisionError: Radius cannot be zero. """ - GRAVITATIONAL_CONSTANT = 6.67430e-11 # m^3 kg^-1 s^-2 + gravitational_constant = 6.67430e-11 # m^3 kg^-1 s^-2 if radius == 0: raise ZeroDivisionError("Radius cannot be zero.") - velocity = math.sqrt(2 * GRAVITATIONAL_CONSTANT * mass / radius) + velocity = math.sqrt(2 * gravitational_constant * mass / radius) return round(velocity, 3) From 7cceb7ec95f8fdad4040cfb13db32beab46ef999 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 10 May 2025 23:40:25 +0300 Subject: [PATCH 18/18] Update escape_velocity.py --- physics/escape_velocity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/escape_velocity.py b/physics/escape_velocity.py index 4cc7c5290dd3..e54ed5e50798 100644 --- a/physics/escape_velocity.py +++ b/physics/escape_velocity.py @@ -56,7 +56,7 @@ def escape_velocity(mass: float, radius: float) -> float: try: mass = float(input("Enter mass of the celestial body (in kgs): ").strip()) - radius = float(input("Enter radius from center (in meters): ").strip()) + radius = float(input("Enter radius from the center of mass (in ms): ").strip()) velocity = escape_velocity(mass=mass, radius=radius) print(f"Escape velocity is {velocity} m/s")