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 t
(mass: float, radius: float)
| 2 | |
| 3 | |
| 4 | def escape_velocity(mass: float, radius: float) -> float: |
| 5 | """ |
| 6 | Calculates the escape velocity needed to break free from a celestial body's |
| 7 | gravitational field. |
| 8 | |
| 9 | The formula used is: |
| 10 | v = sqrt(2 * G * M / R) |
| 11 | |
| 12 | where: |
| 13 | v = escape velocity (m/s) |
| 14 | G = gravitational constant (6.67430 * 10^-11 m^3 kg^-1 s^-2) |
| 15 | M = mass of the celestial body (kg) |
| 16 | R = radius from the center of mass (m) |
| 17 | |
| 18 | Source: |
| 19 | https://en.wikipedia.org/wiki/Escape_velocity |
| 20 | |
| 21 | Args: |
| 22 | mass (float): Mass of the celestial body in kilograms. |
| 23 | radius (float): Radius from the center of mass in meters. |
| 24 | |
| 25 | Returns: |
| 26 | float: Escape velocity in meters per second, rounded to 3 decimal places. |
| 27 | |
| 28 | Examples: |
| 29 | >>> escape_velocity(mass=5.972e24, radius=6.371e6) # Earth |
| 30 | 11185.978 |
| 31 | >>> escape_velocity(mass=7.348e22, radius=1.737e6) # Moon |
| 32 | 2376.307 |
| 33 | >>> escape_velocity(mass=1.898e27, radius=6.9911e7) # Jupiter |
| 34 | 60199.545 |
| 35 | >>> escape_velocity(mass=0, radius=1.0) |
| 36 | 0.0 |
| 37 | >>> escape_velocity(mass=1.0, radius=0) |
| 38 | Traceback (most recent call last): |
| 39 | ... |
| 40 | ZeroDivisionError: Radius cannot be zero. |
| 41 | """ |
| 42 | gravitational_constant = 6.67430e-11 # m^3 kg^-1 s^-2 |
| 43 | |
| 44 | if radius == 0: |
| 45 | raise ZeroDivisionError("Radius cannot be zero.") |
| 46 | |
| 47 | velocity = math.sqrt(2 * gravitational_constant * mass / radius) |
| 48 | return round(velocity, 3) |
| 49 | |
| 50 | |
| 51 | if __name__ == "__main__": |