-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
#9191 mass energy eqivalence #9217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
mhsuhail00
wants to merge
31
commits into
TheAlgorithms:master
from
mhsuhail00:#9191_mass_energy_eqivalence
Closed
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
4efd5ec
Create mass_energy_equivalence.py
mhsuhail00 6acde00
Update mass_energy_equivalence.py
mhsuhail00 63cf15d
Update mass_energy_equivalence.py
mhsuhail00 b7773d2
Update mass_energy_equivalence.py
mhsuhail00 79ccf5a
Update mass_energy_equivalence.py
mhsuhail00 5a3e966
Update mass_energy_equivalence.py
mhsuhail00 fdb0da5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4bc3fa1
Update mass_energy_equivalence.py
mhsuhail00 55c8e1f
Update mass_energy_equivalence.py
mhsuhail00 dbd065c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 90f562f
Update mass_energy_equivalence.py
mhsuhail00 32ca83f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 396805b
Apply suggestions from code review
mhsuhail00 46268c1
Apply suggestions from code review
mhsuhail00 fc5b639
Apply suggestions from code review
mhsuhail00 e2438a6
Apply suggestions from code review
mhsuhail00 456f39c
Update mass_energy_equivalence.py
mhsuhail00 aedc65a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 832d481
Update mass_energy_equivalence.py
mhsuhail00 888ba00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 393b522
Update mass_energy_equivalence.py
mhsuhail00 2ae7736
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7bca844
Update mass_energy_equivalence.py
mhsuhail00 11a02a6
Update mass_energy_equivalence.py
mhsuhail00 7542a9e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 614a8ec
Update mass_energy_equivalence.py
mhsuhail00 733fdbe
Update mass_energy_equivalence.py
mhsuhail00 a79fbcc
Update mass_energy_equivalence.py
mhsuhail00 91b1445
Update mass_energy_equivalence.py
mhsuhail00 39969f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9c1e532
Update mass_energy_equivalence.py
mhsuhail00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Mass-energy equivalence | ||
Reference: https://en.wikipedia.org/wiki/Mass%E2%80%93energy_equivalence | ||
|
||
Here it defines a Function to calculate energy(in joules) equivalent of a given | ||
mass(in kilograms) using Einstein's Mass-energy equivalence equation E=mc^2 | ||
where m is mass and c is the speed of light in vaccume(a constant value) in meter per second. | ||
|
||
The function takes a single argument mass(in kg) having datatype float and | ||
returns the energy(in joule) having datatype float | ||
""" | ||
|
||
|
||
def calculate_energy(mass: float) -> float: | ||
""" | ||
get energy equivalent of given mass in joules: | ||
>>> calculate_energy(1.2) | ||
1.0785062144841811e+17 | ||
>>> calculate_energy(3.0) | ||
2.6962655362104528e+17 | ||
>>> calculate_energy(5) | ||
4.493775893684088e+17 | ||
>>> calculate_energy(-5) | ||
Traceback (most recent call last): | ||
... | ||
raise TypeError("Mass cannot be negative") | ||
TypeError: Mass cannot be negative | ||
""" | ||
if mass < 0: | ||
raise TypeError("Mass cannot be negative") | ||
speed_of_light = 299792458 | ||
energy = mass * speed_of_light**2 | ||
return energy | ||
|
||
|
||
if __name__ == "__main__": | ||
mass = float(input("Enter mass (in kilograms): ")) | ||
energy = calculate_energy(mass) | ||
print(f"Energy equivalent: {energy} joules") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a description of the function, so please put it in the function's docstring rather than the header string.