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:
Du Yuanchao 2020-09-22 21:15:11 +08:00 committed by GitHub
parent 718be54dbb
commit a1ea76bcf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 7 deletions

View File

@ -713,6 +713,7 @@
## Strings
* [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py)
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
* [Can String Be Rearranged As Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/can_string_be_rearranged_as_palindrome.py)
* [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py)
* [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py)
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)

View File

@ -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()

View File

@ -25,11 +25,10 @@ def solution():
# 31875000
"""
return [
a * b * c
a * b * (1000 - a - b)
for a in range(1, 999)
for b in range(a, 999)
for c in range(b, 999)
if (a * a + b * b == c * c) and (a + b + c == 1000)
if (a * a + b * b == (1000 - a - b) ** 2)
][0]

View File

@ -8,7 +8,9 @@ from timeit import timeit
# Counter is faster for long strings and non-Counter is faster for short strings.
def can_string_be_rearranged_as_palindrome_counter(input_str: str = "",) -> bool:
def can_string_be_rearranged_as_palindrome_counter(
input_str: str = "",
) -> bool:
"""
A Palindrome is a String that reads the same forward as it does backwards.
Examples of Palindromes mom, dad, malayalam