|
| 1 | +""" |
| 2 | +Title : Finding the value of either Gravitational Force, one of the masses or distance |
| 3 | +provided that the other three parameters are given. |
| 4 | +
|
| 5 | +Description : Newton's Law of Universal Gravitation explains the presence of force of |
| 6 | +attraction between bodies having a definite mass situated at a distance. It is usually |
| 7 | +stated as that, every particle attracts every other particle in the universe with a |
| 8 | +force that is directly proportional to the product of their masses and inversely |
| 9 | +proportional to the square of the distance between their centers. The publication of the |
| 10 | +theory has become known as the "first great unification", as it marked the unification |
| 11 | +of the previously described phenomena of gravity on Earth with known astronomical |
| 12 | +behaviors. |
| 13 | +
|
| 14 | +The equation for the universal gravitation is as follows: |
| 15 | +F = (G * mass_1 * mass_2) / (distance)^2 |
| 16 | +
|
| 17 | +Source : |
| 18 | +- https://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation |
| 19 | +- Newton (1687) "Philosophiæ Naturalis Principia Mathematica" |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +# Define the Gravitational Constant G and the function |
| 25 | +GRAVITATIONAL_CONSTANT = 6.6743e-11 # unit of G : m^3 * kg^-1 * s^-2 |
| 26 | + |
| 27 | + |
| 28 | +def gravitational_law( |
| 29 | + force: float, mass_1: float, mass_2: float, distance: float |
| 30 | +) -> dict[str, float]: |
| 31 | + |
| 32 | + """ |
| 33 | + Input Parameters |
| 34 | + ---------------- |
| 35 | + force : magnitude in Newtons |
| 36 | +
|
| 37 | + mass_1 : mass in Kilograms |
| 38 | +
|
| 39 | + mass_2 : mass in Kilograms |
| 40 | +
|
| 41 | + distance : distance in Meters |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + result : dict name, value pair of the parameter having Zero as it's value |
| 46 | +
|
| 47 | + Returns the value of one of the parameters specified as 0, provided the values of |
| 48 | + other parameters are given. |
| 49 | + >>> gravitational_law(force=0, mass_1=5, mass_2=10, distance=20) |
| 50 | + {'force': 8.342875e-12} |
| 51 | +
|
| 52 | + >>> gravitational_law(force=7367.382, mass_1=0, mass_2=74, distance=3048) |
| 53 | + {'mass_1': 1.385816317292268e+19} |
| 54 | +
|
| 55 | + >>> gravitational_law(force=36337.283, mass_1=0, mass_2=0, distance=35584) |
| 56 | + Traceback (most recent call last): |
| 57 | + ... |
| 58 | + ValueError: One and only one argument must be 0 |
| 59 | +
|
| 60 | + >>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584) |
| 61 | + Traceback (most recent call last): |
| 62 | + ... |
| 63 | + ValueError: Mass can not be negative |
| 64 | +
|
| 65 | + >>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374) |
| 66 | + Traceback (most recent call last): |
| 67 | + ... |
| 68 | + ValueError: Gravitational force can not be negative |
| 69 | + """ |
| 70 | + |
| 71 | + product_of_mass = mass_1 * mass_2 |
| 72 | + |
| 73 | + if (force, mass_1, mass_2, distance).count(0) != 1: |
| 74 | + raise ValueError("One and only one argument must be 0") |
| 75 | + if force < 0: |
| 76 | + raise ValueError("Gravitational force can not be negative") |
| 77 | + if distance < 0: |
| 78 | + raise ValueError("Distance can not be negative") |
| 79 | + if mass_1 < 0 or mass_2 < 0: |
| 80 | + raise ValueError("Mass can not be negative") |
| 81 | + if force == 0: |
| 82 | + force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2) |
| 83 | + return {"force": force} |
| 84 | + elif mass_1 == 0: |
| 85 | + mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2) |
| 86 | + return {"mass_1": mass_1} |
| 87 | + elif mass_2 == 0: |
| 88 | + mass_2 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_1) |
| 89 | + return {"mass_2": mass_2} |
| 90 | + elif distance == 0: |
| 91 | + distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5 |
| 92 | + return {"distance": distance} |
| 93 | + raise ValueError("One and only one argument must be 0") |
| 94 | + |
| 95 | + |
| 96 | +# Run doctest |
| 97 | +if __name__ == "__main__": |
| 98 | + import doctest |
| 99 | + |
| 100 | + doctest.testmod() |
0 commit comments