From 1a43c92c77790632e7958df9c1048ed77aa36f68 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 5 Nov 2021 22:44:24 +0300 Subject: [PATCH] Improve Project Euler problem 043 solution 1 (#5772) * updating DIRECTORY.md * Fix typo * Improve solution Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 3 +++ project_euler/problem_043/sol1.py | 21 ++++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index fd164c92e..32ca8cd3b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/project_euler/problem_043/sol1.py b/project_euler/problem_043/sol1.py index 1febe4a4d..c533f40da 100644 --- a/project_euler/problem_043/sol1.py +++ b/project_euler/problem_043/sol1.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__":