Skip to content

Commit 1a43c92

Browse files
MaximSmolskiygithub-actions
and
github-actions
authored
Improve Project Euler problem 043 solution 1 (TheAlgorithms#5772)
* updating DIRECTORY.md * Fix typo * Improve solution Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 4896026 commit 1a43c92

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

DIRECTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253

254254
## Dynamic Programming
255255
* [Abbreviation](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/abbreviation.py)
256+
* [All Construct](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/all_construct.py)
256257
* [Bitmask](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/bitmask.py)
257258
* [Catalan Numbers](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/catalan_numbers.py)
258259
* [Climbing Stairs](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/climbing_stairs.py)
@@ -612,6 +613,7 @@
612613

613614
## Physics
614615
* [N Body Simulation](https://github.com/TheAlgorithms/Python/blob/master/physics/n_body_simulation.py)
616+
* [Newtons Second Law Of Motion](https://github.com/TheAlgorithms/Python/blob/master/physics/newtons_second_law_of_motion.py)
615617

616618
## Project Euler
617619
* Problem 001
@@ -1018,6 +1020,7 @@
10181020
* [Nasa Data](https://github.com/TheAlgorithms/Python/blob/master/web_programming/nasa_data.py)
10191021
* [Random Anime Character](https://github.com/TheAlgorithms/Python/blob/master/web_programming/random_anime_character.py)
10201022
* [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py)
1023+
* [Reddit](https://github.com/TheAlgorithms/Python/blob/master/web_programming/reddit.py)
10211024
* [Search Books By Isbn](https://github.com/TheAlgorithms/Python/blob/master/web_programming/search_books_by_isbn.py)
10221025
* [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py)
10231026
* [Test Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/test_fetch_github_info.py)

project_euler/problem_043/sol1.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,34 @@ def is_substring_divisible(num: tuple) -> bool:
3333
>>> is_substring_divisible((1, 4, 0, 6, 3, 5, 7, 2, 8, 9))
3434
True
3535
"""
36-
tests = [2, 3, 5, 7, 11, 13, 17]
36+
if num[3] % 2 != 0:
37+
return False
38+
39+
if (num[2] + num[3] + num[4]) % 3 != 0:
40+
return False
41+
42+
if num[5] % 5 != 0:
43+
return False
44+
45+
tests = [7, 11, 13, 17]
3746
for i, test in enumerate(tests):
38-
if (num[i + 1] * 100 + num[i + 2] * 10 + num[i + 3]) % test != 0:
47+
if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0:
3948
return False
4049
return True
4150

4251

4352
def solution(n: int = 10) -> int:
4453
"""
4554
Returns the sum of all pandigital numbers which pass the
46-
divisiility tests.
55+
divisibility tests.
4756
>>> solution(10)
4857
16695334890
4958
"""
50-
list_nums = [
59+
return sum(
5160
int("".join(map(str, num)))
5261
for num in permutations(range(n))
5362
if is_substring_divisible(num)
54-
]
55-
56-
return sum(list_nums)
63+
)
5764

5865

5966
if __name__ == "__main__":

0 commit comments

Comments
 (0)