Skip to content

Optimization problem_10 in project_euler #2453

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

Merged
merged 7 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 36 additions & 3 deletions project_euler/problem_09/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
5 changes: 2 additions & 3 deletions project_euler/problem_09/sol3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
4 changes: 3 additions & 1 deletion strings/can_string_be_rearranged_as_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
# 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
Expand Down