mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Optimization problem_10 in project_euler (#2453)
* optimization for problem09 in project_euler * added benchmark code * fixup! Format Python code with psf/black push * Update project_euler/problem_09/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * updating DIRECTORY.md * Update project_euler/problem_09/sol1.py * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
@ -16,7 +16,6 @@ def solution():
|
||||
1. a < b < c
|
||||
2. a**2 + b**2 = c**2
|
||||
3. a + b + c = 1000
|
||||
|
||||
# The code below has been commented due to slow execution affecting Travis.
|
||||
# >>> solution()
|
||||
# 31875000
|
||||
@ -30,6 +29,40 @@ def solution():
|
||||
return a * b * c
|
||||
|
||||
|
||||
def solution_fast():
|
||||
"""
|
||||
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
|
||||
the following:
|
||||
1. a < b < c
|
||||
2. a**2 + b**2 = c**2
|
||||
3. a + b + c = 1000
|
||||
|
||||
# The code below has been commented due to slow execution affecting Travis.
|
||||
# >>> solution_fast()
|
||||
# 31875000
|
||||
"""
|
||||
for a in range(300):
|
||||
for b in range(400):
|
||||
c = 1000 - a - b
|
||||
if a < b < c and (a ** 2) + (b ** 2) == (c ** 2):
|
||||
return a * b * c
|
||||
|
||||
|
||||
def benchmark() -> None:
|
||||
"""
|
||||
Benchmark code comparing two different version function.
|
||||
"""
|
||||
import timeit
|
||||
|
||||
print(
|
||||
timeit.timeit("solution()", setup="from __main__ import solution", number=1000)
|
||||
)
|
||||
print(
|
||||
timeit.timeit(
|
||||
"solution_fast()", setup="from __main__ import solution_fast", number=1000
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Please Wait...")
|
||||
print(solution())
|
||||
benchmark()
|
||||
|
Reference in New Issue
Block a user