Skip to content

[pull] master from TheAlgorithms:master #44

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

Open
wants to merge 135 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
03a4251
[pre-commit.ci] pre-commit autoupdate (#12071)
pre-commit-ci[bot] Oct 14, 2024
6e24935
[pre-commit.ci] pre-commit autoupdate (#12234)
pre-commit-ci[bot] Oct 22, 2024
52602ea
[pre-commit.ci] pre-commit autoupdate (#12313)
pre-commit-ci[bot] Oct 28, 2024
a19bede
Add scripts/find_git_conflicts.sh (#12343)
cclauss Nov 1, 2024
3e9ca92
[pre-commit.ci] pre-commit autoupdate (#12349)
pre-commit-ci[bot] Nov 4, 2024
e3f3d66
[pre-commit.ci] pre-commit autoupdate (#12370)
pre-commit-ci[bot] Nov 11, 2024
e3bd772
`validate_filenames.py` Shebang `python` for Windows (#12371)
cclauss Nov 15, 2024
f3f32ae
[pre-commit.ci] pre-commit autoupdate (#12385)
pre-commit-ci[bot] Nov 18, 2024
fc33c50
[pre-commit.ci] pre-commit autoupdate (#12398)
pre-commit-ci[bot] Nov 25, 2024
c792122
Added matrix based color game algorithm (#12400)
Miranda13 Dec 2, 2024
b22fab0
[pre-commit.ci] pre-commit autoupdate (#12404)
pre-commit-ci[bot] Dec 2, 2024
0bcdfbd
Use Astral uv (#12402)
cclauss Dec 5, 2024
98391e3
[pre-commit.ci] pre-commit autoupdate (#12428)
pre-commit-ci[bot] Dec 12, 2024
f8e595e
[pre-commit.ci] pre-commit autoupdate (#12439)
pre-commit-ci[bot] Dec 16, 2024
4abfce2
Fix sphinx/build_docs warnings for audio_filters (#12449)
MaximSmolskiy Dec 23, 2024
47cd21a
Fix sphinx/build_docs warnings for cellular_automata (#12454)
MaximSmolskiy Dec 23, 2024
c5e603a
Fix sphinx/build_docs warnings for geodesy (#12462)
MaximSmolskiy Dec 23, 2024
b0cb13f
Fix sphinx/build_docs warnings for greedy_methods (#12463)
MaximSmolskiy Dec 23, 2024
04fbfd6
Fix sphinx/build_docs warnings for maths/volume (#12464)
MaximSmolskiy Dec 24, 2024
e9721aa
Fix sphinx/build_docs warnings for physics/horizontal_projectile_moti…
MaximSmolskiy Dec 24, 2024
c36aaf0
Fix sphinx/build_docs warnings for graphs/check_bipatrite (#12469)
MaximSmolskiy Dec 24, 2024
ae28fa7
Fix sphinx/build_docs warnings for data_structures/binary_tree/mirror…
MaximSmolskiy Dec 24, 2024
eb652cf
Bump astral-sh/setup-uv from 4 to 5 (#12445)
dependabot[bot] Dec 27, 2024
5bef6ac
Improve coverage special_numbers (#12414)
Scarfinos Dec 27, 2024
8bbe8ca
Improve test coverage for matrix exponentiation (#12388)
Scarfinos Dec 27, 2024
7647181
Improve test coverage for armstrong numbers (#12327)
Scarfinos Dec 27, 2024
2ae9534
Added test to linear regression (#12353)
Miranda13 Dec 27, 2024
1652d05
adding test to electronics/electric_power.py (#12387)
juliaaragao Dec 27, 2024
929b7dc
Fix Gaussian elimination pivoting (#11393)
MaximSmolskiy Dec 28, 2024
b5c8fbf
Add additional doctests, fix grammatical errors for maths/perfect_num…
joy-programs Dec 28, 2024
d496d56
Remove inaccessible URL in computer_vision/README.md (#12383)
setbit123 Dec 28, 2024
1909f22
adding doctests to maths/trapezoidal_rule.py (#12193)
MRJPEREZR Dec 28, 2024
2b58ab0
compare-method added to Vector class in lib.py (#12448)
Andrwaa Dec 28, 2024
2d68bb5
Fix split function to handle trailing delimiters correctly (#12423)
kishyassin Dec 29, 2024
972a5c1
fixed the issue in strings/join.py (#12434)
RajdeepBakolia2004 Dec 29, 2024
d9092d8
fixes requirements error (#12438)
sanks011 Dec 29, 2024
bfc804a
Fix sphinx/build_docs warnings for physics/newtons_second_law_of_moti…
MaximSmolskiy Dec 29, 2024
c932883
[pre-commit.ci] pre-commit autoupdate (#12466)
pre-commit-ci[bot] Dec 29, 2024
bfb0447
Fix sphinx/build_docs warnings for maths/zellers_congruence (#12481)
MaximSmolskiy Dec 29, 2024
ce036db
Fix sphinx/build_docs warnings for physics/speeds_of_gas_molecules (#…
MaximSmolskiy Dec 29, 2024
3622e94
Fix sphinx/build_docs warnings for other (#12482)
MaximSmolskiy Dec 29, 2024
94b3777
Fix sphinx/build_docs warnings for linear_algebra (#12483)
MaximSmolskiy Dec 29, 2024
f45e392
Fix sphinx/build_docs warnings for ciphers (#12485)
MaximSmolskiy Dec 30, 2024
68b4c6b
fix is_ip_v4_address_valid.py (#12394)
mahditavasoli Dec 30, 2024
2ca96b7
current_stock_price test added (#12390)
MRJPEREZR Dec 30, 2024
24923ee
Add doctest to maths/numerical_analysis/intersection.py (#12148)
shikhar-sm Dec 30, 2024
da587d0
Added doctest to /maths/power_using_recursion.py (#11994)
Parthjhalani07 Dec 30, 2024
493a7c1
feat: add testcase of assemble_transformation (#11810)
zeelrupapara Dec 30, 2024
7fa9b4b
Fix sphinx/build_docs warnings for dynamic_programming (#12484)
MaximSmolskiy Dec 30, 2024
a2be5ad
Tests electronics/electric_conductivity.py #9943 (#12437)
juliaaragao Dec 30, 2024
f24ddba
Implemented doctests for geometry-related classes (#12368)
IsxImattI Dec 30, 2024
7742536
Intensity_based_Segmentation (#12491)
AtharvMalusare Dec 30, 2024
75c5c41
#9943 : Adding coverage test for basic_graphs.py (#12354)
Scarfinos Dec 30, 2024
7e55fb6
- Implemented `find_lanczos_eigenvectors` to approximate the largest …
jeffreyyancey Dec 30, 2024
8921b56
Adding tests to monotonic_array.py (#12073)
MelihMSahin Dec 30, 2024
5942059
add doctest for quick_sort_3_partition (#11779)
lorduke22 Dec 30, 2024
8767d1d
add some documentation for heap sort (#9949)
rodrigondec Dec 31, 2024
8439fa8
Added the algorithm to compute the time period of a simple pendulum (…
pluto-tofu Dec 31, 2024
a439902
chore: improve comments and add tests to trapezoidal rule (#11640)
jurichar Dec 31, 2024
91a22c2
Create digital differential analyzer_line.py (#10929)
nababuddin Dec 31, 2024
12b1023
[ADDED] Implementation of Geometric Mean. (#10421)
kmtGryffindor20 Dec 31, 2024
bae33ac
[pre-commit.ci] pre-commit autoupdate (#12507)
pre-commit-ci[bot] Jan 6, 2025
b653aee
Fix ruff (#12515)
MaximSmolskiy Jan 12, 2025
4c92de5
Fix dynamic_programming/longest_increasing_subsequence.py (#12517)
XenoBytesX Jan 12, 2025
787aa5d
doctest all_combinations.py (#12506)
Siddhant231xyz Jan 13, 2025
cfcc84e
Fix build (#12516)
MaximSmolskiy Jan 13, 2025
4fe50bc
[pre-commit.ci] pre-commit autoupdate -- ruff 2025 stable format (#12…
pre-commit-ci[bot] Jan 13, 2025
f04d308
Create longest_increasing_subsequence_iterative.py (#12524)
XenoBytesX Jan 14, 2025
0040ad4
Add butterfly pattern implementation (#12493)
aydinomer00 Jan 14, 2025
533767f
Doomsday Algorithm: Fix leap year check (#12396)
minh-swinburne Jan 18, 2025
91ebea1
Sphinx runs on ubuntu 24.04 arm (#12530)
cclauss Jan 19, 2025
1f74db0
[pre-commit.ci] pre-commit autoupdate (#12536)
pre-commit-ci[bot] Jan 20, 2025
9fb51b4
Update docstrings in the functions definitions. (#11797)
ronaldngounou Jan 23, 2025
c666db3
Add Doc test bubble sort (#12070)
vijayalaxmi777 Jan 23, 2025
13e4d3e
Fix error in avl_tree del_node function (#11510)
Rosepetal2022 Jan 24, 2025
6c92c5a
[pre-commit.ci] pre-commit autoupdate (#12542)
pre-commit-ci[bot] Jan 27, 2025
e59d819
[pre-commit.ci] pre-commit autoupdate (#12554)
pre-commit-ci[bot] Feb 5, 2025
338cbaf
Improve power.py (#12567)
lighting9999 Feb 9, 2025
738253e
git mv data_structures/queue data_structures/queues (#12577)
pre-commit-ci[bot] Feb 14, 2025
a5aed92
fix: typo in data_structures/linked_list/from_sequence.py (#12584)
maximevtush Feb 20, 2025
183fa06
Fix n-queens problem (#12583)
infrablue1 Feb 22, 2025
114d428
[pre-commit.ci] pre-commit autoupdate (#12591)
pre-commit-ci[bot] Feb 24, 2025
f528ce3
Added dynamic_programming/range_sum_query.py (#12592)
XenoBytesX Feb 27, 2025
8826ad3
feat: Implement Principal Component Analysis (PCA) (#12596)
parikshit2111 Mar 2, 2025
fff34ed
[pre-commit.ci] pre-commit autoupdate (#12599)
pre-commit-ci[bot] Mar 3, 2025
a415a95
Add largest rectangle histogram (#12269)
ankana2113 Mar 8, 2025
4fbd350
Improved test coverage in decimal_to_fraction.py (#12608)
PaulAdutwum Mar 8, 2025
e3fb530
Improve decimal_to_fraction.py (#12611)
PaulAdutwum Mar 8, 2025
23eb174
[pre-commit.ci] pre-commit autoupdate (#12614)
pre-commit-ci[bot] Mar 11, 2025
7ce998b
Fix some RUF012 per file ignores (#11399)
MaximSmolskiy Mar 11, 2025
edf7c37
[pre-commit.ci] pre-commit autoupdate (#12623)
pre-commit-ci[bot] Mar 18, 2025
580273e
Improve prefix_sum.py (#12560)
pranjaykumar926 Mar 19, 2025
e3773db
[pre-commit.ci] pre-commit autoupdate (#12631)
pre-commit-ci[bot] Mar 24, 2025
74b540a
Genetic Algorithm: Fix bug in multi-threading (#12644)
Dang-Hoang-Tung Mar 29, 2025
f10a5cb
prefix_evaluation: Add alternative recursive implementation (#12646)
isidroas Mar 31, 2025
baab802
doubly linked list: add dataclass and typing (#12647)
isidroas Apr 1, 2025
0c8cf8e
Fix bug for data_structures/linked_list/doubly_linked_list_two.py (#1…
MaximSmolskiy Apr 2, 2025
5afe029
[pre-commit.ci] pre-commit autoupdate (#12661)
pre-commit-ci[bot] Apr 7, 2025
a4576dc
fix: correct typo "util" to "until" (#12653)
kimtth Apr 9, 2025
4ed6141
[pre-commit.ci] pre-commit autoupdate (#12671)
pre-commit-ci[bot] Apr 14, 2025
cc621f1
Add find_unique_number algorithm to bit manipulation (#12654)
parth-6945 Apr 14, 2025
d123cbc
Solution for the Euler Project Problem 122 (#12655)
mindaugl Apr 14, 2025
4282063
Add matrix inversion algorithm using NumPy (#12657)
NaitikDwivede Apr 14, 2025
c585cb1
Solution for the Euler Project problem 136 (#12658)
mindaugl Apr 17, 2025
a1aa631
Adding time and a half pay calculator algorithm to financial folder (…
konsoleSam Apr 17, 2025
9891d2b
[pre-commit.ci] pre-commit autoupdate (#12680)
pre-commit-ci[bot] Apr 21, 2025
11a61d1
Generic type hint in DDL (#12677)
isidroas Apr 21, 2025
29afed0
Bump astral-sh/setup-uv from 5 to 6 (#12683)
dependabot[bot] Apr 25, 2025
0a3a965
[pre-commit.ci] pre-commit autoupdate (#12692)
pre-commit-ci[bot] Apr 28, 2025
145879b
Add solution for the Euler project problem 164. (#12663)
mindaugl May 5, 2025
40f4c51
Add solution for the Euler problem 190 (#12664)
mindaugl May 5, 2025
7ed7f04
[pre-commit.ci] pre-commit autoupdate (#12708)
pre-commit-ci[bot] May 5, 2025
d9d56b1
Add solution for the Euler project problem 345 (#12666)
mindaugl May 6, 2025
b720f24
Add solution for the Euler project problem 95. (#12669)
mindaugl May 10, 2025
a728cc9
Added/Improved doctests for lowest_common_ancestor.py (#12673)
NidhaNureen May 10, 2025
59c3c8b
Add N Input AND Gate (#12717)
robohie May 10, 2025
47a44ab
Improve longest_common_substring.py (#12705)
Alfredoxrock May 10, 2025
1317655
Fix error messages for horizontal_projectile_motion.py (#12722)
robohie May 10, 2025
95fb181
Add escape velocity calculator using standard physics formula (#12721)
SajeevSenthil May 10, 2025
1ea05fe
Prep for Python 3.14: Rename compression to data_compression (#12725)
cclauss May 12, 2025
088c74e
Delete empty source directory (#12730)
cclauss May 12, 2025
485f688
Add PEP723 header to scripts/validate_solutions.py (#12731)
cclauss May 12, 2025
f721e59
Add a proper shebang line to scripts/validate_filenames.py (#12733)
cclauss May 12, 2025
ee3a173
[pre-commit.ci] pre-commit autoupdate (#12736)
pre-commit-ci[bot] May 12, 2025
6e4d1b3
Physics orbital_transfer_work (#12728)
SajeevSenthil May 13, 2025
a2fa32c
Lukazlim: Replace dependency `requests` with `httpx` (#12744)
cclauss May 14, 2025
26ad689
Add median() function using Quickselect (#12676)
isidroas May 21, 2025
ca445f5
Add bidirectional search algorithm implementation (#12649)
prajwalc22 May 22, 2025
c81cc26
Improve hash map (#12678)
isidroas May 22, 2025
e1115b5
Add tests and cleanup sum_of_subsets algorithm (#12746)
mindaugl May 22, 2025
a8ad2db
Update matrix_chain_order calculation with more details and test. (#1…
mindaugl May 22, 2025
e26c388
[pre-commit.ci] pre-commit autoupdate (#12760)
pre-commit-ci[bot] May 26, 2025
d0d7f0b
maths/radix2_fft.py: Fix calculation for Python 3.14 (#12772)
cclauss May 29, 2025
4b077c0
feat: create straight_line_depreciation (#12377)
kangaroo-eating-carrots May 31, 2025
c3d4b9e
Add doctests for the boyer_moore_search algorithm. (#12769)
mindaugl Jun 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add escape velocity calculator using standard physics formula (TheAlg…
…orithms#12721)

* Added iterative solution for power calculation

* Added iterative solution for power calculation

* Added iterative solution for power calculation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added iterative solution for power calculation fixes TheAlgorithms#12709

* Added iterative solution for power calculation  FIXES NUMBER 12709

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* 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)

* Fix: added header comment to escape_velocity.py

* Trigger re-PR with a minor change

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix: resolve Ruff linter errors and add Wikipedia reference

* Delete maths/power_using_iteration.py

* Test doctests

* Update escape_velocity.py

* Update escape_velocity.py

* Update escape_velocity.py

* Update escape_velocity.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
  • Loading branch information
3 people authored May 10, 2025
commit 95fb181f5a944427fdbc5766cbf4e1cb699d4a6d
67 changes: 67 additions & 0 deletions physics/escape_velocity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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)

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.

Returns:
float: Escape velocity in meters per second, rounded to 3 decimal places.

Examples:
>>> escape_velocity(mass=5.972e24, radius=6.371e6) # Earth
11185.978
>>> escape_velocity(mass=7.348e22, radius=1.737e6) # Moon
2376.307
>>> escape_velocity(mass=1.898e27, radius=6.9911e7) # Jupiter
60199.545
>>> escape_velocity(mass=0, radius=1.0)
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

if radius == 0:
raise ZeroDivisionError("Radius cannot be zero.")

velocity = math.sqrt(2 * gravitational_constant * mass / radius)
return round(velocity, 3)


if __name__ == "__main__":
import doctest

doctest.testmod()
print("Calculate escape velocity of a celestial body...\n")

try:
mass = float(input("Enter mass of the celestial body (in kgs): ").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")

except ValueError:
print("Invalid input. Please enter valid numeric values.")
except ZeroDivisionError as e:
print(e)