未验证 提交 1a43c92c 编写于 作者: M Maxim Smolskiy 提交者: GitHub

Improve Project Euler problem 043 solution 1 (#5772)

* updating DIRECTORY.md

* Fix typo

* Improve solution
Co-authored-by: Ngithub-actions <${GITHUB_ACTOR}@users.noreply.github.com>
上级 48960268
......@@ -253,6 +253,7 @@
## Dynamic Programming
* [Abbreviation](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/abbreviation.py)
* [All Construct](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/all_construct.py)
* [Bitmask](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/bitmask.py)
* [Catalan Numbers](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/catalan_numbers.py)
* [Climbing Stairs](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/climbing_stairs.py)
......@@ -612,6 +613,7 @@
## Physics
* [N Body Simulation](https://github.com/TheAlgorithms/Python/blob/master/physics/n_body_simulation.py)
* [Newtons Second Law Of Motion](https://github.com/TheAlgorithms/Python/blob/master/physics/newtons_second_law_of_motion.py)
## Project Euler
* Problem 001
......@@ -1018,6 +1020,7 @@
* [Nasa Data](https://github.com/TheAlgorithms/Python/blob/master/web_programming/nasa_data.py)
* [Random Anime Character](https://github.com/TheAlgorithms/Python/blob/master/web_programming/random_anime_character.py)
* [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py)
* [Reddit](https://github.com/TheAlgorithms/Python/blob/master/web_programming/reddit.py)
* [Search Books By Isbn](https://github.com/TheAlgorithms/Python/blob/master/web_programming/search_books_by_isbn.py)
* [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py)
* [Test Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/test_fetch_github_info.py)
......
......@@ -33,9 +33,18 @@ def is_substring_divisible(num: tuple) -> bool:
>>> is_substring_divisible((1, 4, 0, 6, 3, 5, 7, 2, 8, 9))
True
"""
tests = [2, 3, 5, 7, 11, 13, 17]
if num[3] % 2 != 0:
return False
if (num[2] + num[3] + num[4]) % 3 != 0:
return False
if num[5] % 5 != 0:
return False
tests = [7, 11, 13, 17]
for i, test in enumerate(tests):
if (num[i + 1] * 100 + num[i + 2] * 10 + num[i + 3]) % test != 0:
if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0:
return False
return True
......@@ -43,17 +52,15 @@ def is_substring_divisible(num: tuple) -> bool:
def solution(n: int = 10) -> int:
"""
Returns the sum of all pandigital numbers which pass the
divisiility tests.
divisibility tests.
>>> solution(10)
16695334890
"""
list_nums = [
return sum(
int("".join(map(str, num)))
for num in permutations(range(n))
if is_substring_divisible(num)
]
return sum(list_nums)
)
if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册