mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-12 23:52:17 +08:00
Optimized recursive_bubble_sort (#2410)
* optimized recursive_bubble_sort * Fixed doctest error due whitespace * reduce loop times for optimization * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -23,8 +23,7 @@ def solution(n):
|
||||
product = -1
|
||||
d = 0
|
||||
for a in range(1, n // 3):
|
||||
"""Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c
|
||||
"""
|
||||
"""Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c"""
|
||||
b = (n * n - 2 * a * n) // (2 * n - 2 * a)
|
||||
c = n - a - b
|
||||
if c * c == (a * a + b * b):
|
||||
|
@ -12,7 +12,7 @@ smaller than n when n is smaller than 10 million. Only for positive numbers.
|
||||
|
||||
|
||||
def prime_sum(n: int) -> int:
|
||||
""" Returns the sum of all the primes below n.
|
||||
"""Returns the sum of all the primes below n.
|
||||
|
||||
>>> prime_sum(2_000_000)
|
||||
142913828922
|
||||
|
@ -8,31 +8,31 @@ from math import factorial
|
||||
|
||||
def lattice_paths(n):
|
||||
"""
|
||||
Returns the number of paths possible in a n x n grid starting at top left
|
||||
corner going to bottom right corner and being able to move right and down
|
||||
only.
|
||||
Returns the number of paths possible in a n x n grid starting at top left
|
||||
corner going to bottom right corner and being able to move right and down
|
||||
only.
|
||||
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50
|
||||
1.008913445455642e+29
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25
|
||||
126410606437752.0
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23
|
||||
8233430727600.0
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15
|
||||
155117520.0
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1
|
||||
2.0
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50
|
||||
1.008913445455642e+29
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25
|
||||
126410606437752.0
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23
|
||||
8233430727600.0
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15
|
||||
155117520.0
|
||||
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1
|
||||
2.0
|
||||
|
||||
>>> lattice_paths(25)
|
||||
126410606437752
|
||||
>>> lattice_paths(23)
|
||||
8233430727600
|
||||
>>> lattice_paths(20)
|
||||
137846528820
|
||||
>>> lattice_paths(15)
|
||||
155117520
|
||||
>>> lattice_paths(1)
|
||||
2
|
||||
>>> lattice_paths(25)
|
||||
126410606437752
|
||||
>>> lattice_paths(23)
|
||||
8233430727600
|
||||
>>> lattice_paths(20)
|
||||
137846528820
|
||||
>>> lattice_paths(15)
|
||||
155117520
|
||||
>>> lattice_paths(1)
|
||||
2
|
||||
|
||||
"""
|
||||
n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1,
|
||||
|
@ -39,17 +39,17 @@ def is_prime(k: int) -> bool:
|
||||
|
||||
def solution(a_limit: int, b_limit: int) -> int:
|
||||
"""
|
||||
>>> solution(1000, 1000)
|
||||
-59231
|
||||
>>> solution(200, 1000)
|
||||
-59231
|
||||
>>> solution(200, 200)
|
||||
-4925
|
||||
>>> solution(-1000, 1000)
|
||||
0
|
||||
>>> solution(-1000, -1000)
|
||||
0
|
||||
"""
|
||||
>>> solution(1000, 1000)
|
||||
-59231
|
||||
>>> solution(200, 1000)
|
||||
-59231
|
||||
>>> solution(200, 200)
|
||||
-4925
|
||||
>>> solution(-1000, 1000)
|
||||
0
|
||||
>>> solution(-1000, -1000)
|
||||
0
|
||||
"""
|
||||
longest = [0, 0, 0] # length, a, b
|
||||
for a in range((a_limit * -1) + 1, a_limit):
|
||||
for b in range(2, b_limit):
|
||||
|
@ -1,18 +1,18 @@
|
||||
def maximum_digital_sum(a: int, b: int) -> int:
|
||||
"""
|
||||
Considering natural numbers of the form, a**b, where a, b < 100,
|
||||
what is the maximum digital sum?
|
||||
:param a:
|
||||
:param b:
|
||||
:return:
|
||||
>>> maximum_digital_sum(10,10)
|
||||
45
|
||||
Considering natural numbers of the form, a**b, where a, b < 100,
|
||||
what is the maximum digital sum?
|
||||
:param a:
|
||||
:param b:
|
||||
:return:
|
||||
>>> maximum_digital_sum(10,10)
|
||||
45
|
||||
|
||||
>>> maximum_digital_sum(100,100)
|
||||
972
|
||||
>>> maximum_digital_sum(100,100)
|
||||
972
|
||||
|
||||
>>> maximum_digital_sum(100,200)
|
||||
1872
|
||||
>>> maximum_digital_sum(100,200)
|
||||
1872
|
||||
"""
|
||||
|
||||
# RETURN the MAXIMUM from the list of SUMs of the list of INT converted from STR of
|
||||
|
Reference in New Issue
Block a user