Python/project_euler
Manpreet Singh 1ebae5d43e
Performance: 75% faster Project Euler 187 (#10503)
* Add comments and wikipedia link in calculate_prime_numbers

* Add improved calculate_prime_numbers

* Separate slow_solution and new_solution

* Use for loops in solution

* Separate while_solution and new solution

* Add performance benchmark

* Add doctest for calculate_prime_numbers

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Removed white space

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-10-15 11:17:22 +02:00
..
problem_001 Misc fixes across multiple algorithms (#6912) 2022-10-16 10:55:38 +05:30
problem_002
problem_003 Unify O(sqrt(N)) is_prime functions under project_euler (#6258) 2022-09-14 09:40:04 +01:00
problem_004 pre-commit: Upgrade psf/black for stable style 2023 (#8110) 2023-02-01 18:44:54 +05:30
problem_005
problem_006
problem_007 Unify O(sqrt(N)) is_prime functions under project_euler (#6258) 2022-09-14 09:40:04 +01:00
problem_008 Add flake8-broken-line to pre-commit (#7338) 2022-10-16 21:50:48 +02:00
problem_009 [Upgrade Ruff] Fix all errors raised from ruff (#8879) 2023-07-22 12:05:10 +02:00
problem_010 refactor: Indent ... for visual purposes (#7744) 2022-10-27 19:42:30 +02:00
problem_011 Add flake8 pluin flake8 bugbear to pre-commit (#7132) 2022-10-13 18:03:06 +02:00
problem_012
problem_013
problem_014 Make some ruff fixes (#8154) 2023-03-01 17:23:33 +01:00
problem_015
problem_016
problem_017
problem_018
problem_019 Make some ruff fixes (#8154) 2023-03-01 17:23:33 +01:00
problem_020 Pyupgrade to Python 3.9 (#4718) 2021-09-07 13:37:03 +02:00
problem_021 Pyupgrade to Python 3.9 (#4718) 2021-09-07 13:37:03 +02:00
problem_022 Rename Project Euler directories and other dependent changes (#3300) 2020-10-15 12:43:28 +05:30
problem_023
problem_024
problem_025 clean of unnecessary checks, imports, calls (#7993) 2022-11-21 00:00:27 +13:00
problem_026 Add flake8 pluin flake8 bugbear to pre-commit (#7132) 2022-10-13 18:03:06 +02:00
problem_027
problem_028
problem_029
problem_030 perf: improve Project Euler problem 030 solution 1 (#6267) 2022-07-25 22:11:12 +05:30
problem_031 Rename Project Euler directories and other dependent changes (#3300) 2020-10-15 12:43:28 +05:30
problem_032 Add pep8-naming to pre-commit hooks and fixes incorrect naming conventions (#7062) 2022-10-13 00:54:20 +02:00
problem_033
problem_034
problem_035 [pre-commit.ci] pre-commit autoupdate (#9543) 2023-10-07 21:32:28 +02:00
problem_036
problem_037
problem_038 Pyupgrade to Python 3.9 (#4718) 2021-09-07 13:37:03 +02:00
problem_039 Rename Project Euler directories and other dependent changes (#3300) 2020-10-15 12:43:28 +05:30
problem_040 Rename Project Euler directories and other dependent changes (#3300) 2020-10-15 12:43:28 +05:30
problem_041
problem_042 Misc fixes across multiple algorithms (#6912) 2022-10-16 10:55:38 +05:30
problem_043 Improve Project Euler problem 043 solution 1 (#5772) 2021-11-05 20:44:24 +01:00
problem_044 [mypy] fix type annotations for all Project Euler problems (#4747) 2021-10-12 00:33:44 +08:00
problem_045
problem_046 Unify O(sqrt(N)) is_prime functions under project_euler (#6258) 2022-09-14 09:40:04 +01:00
problem_047
problem_048 The black formatter is no longer beta (#5960) 2022-01-30 20:29:54 +01:00
problem_049
problem_050 The black formatter is no longer beta (#5960) 2022-01-30 20:29:54 +01:00
problem_051 The black formatter is no longer beta (#5960) 2022-01-30 20:29:54 +01:00
problem_052 Add Flake8 comprehensions to pre-commit (#7235) 2022-10-15 19:29:42 +02:00
problem_053
problem_054 Ruff pandas vet (#10281) 2023-10-11 14:30:02 -04:00
problem_055
problem_056 The black formatter is no longer beta (#5960) 2022-01-30 20:29:54 +01:00
problem_057
problem_058 Unify O(sqrt(N)) is_prime functions under project_euler (#6258) 2022-09-14 09:40:04 +01:00
problem_059 Pyupgrade to Python 3.9 (#4718) 2021-09-07 13:37:03 +02:00
problem_062
problem_063
problem_064 Make some ruff fixes (#8154) 2023-03-01 17:23:33 +01:00
problem_065 Add flake8-builtins to pre-commit and fix errors (#7105) 2022-10-13 19:53:59 +05:30
problem_067
problem_068
problem_069
problem_070 Euler 070 partial replacement of numpy loops. (#9055) 2023-09-11 12:11:22 +02:00
problem_071 Rename Project Euler directories and other dependent changes (#3300) 2020-10-15 12:43:28 +05:30
problem_072
problem_073 feat: add Project Euler problem 073 solution 1 (#6273) 2022-10-24 18:59:49 +05:30
problem_074
problem_075
problem_076 Rename Project Euler directories and other dependent changes (#3300) 2020-10-15 12:43:28 +05:30
problem_077
problem_078 Improve Project Euler problem 078 solution 1 (#5708) 2021-10-31 11:36:53 +01:00
problem_079 Add Project Euler problem 79 solution 1 (#8607) 2023-04-02 17:34:11 +05:30
problem_080
problem_081
problem_082 Add Project Euler problem 082 solution 1 (#6282) 2023-03-02 10:25:47 +05:30
problem_085
problem_086
problem_087
problem_089
problem_091 Add solution for Project Euler problem 91. (#3144) 2020-10-16 15:44:06 +05:30
problem_092
problem_094
problem_097 refactor: Move constants outside of variable scope (#7262) 2022-10-16 15:03:29 +05:30
problem_099 [mypy] fix type annotations for all Project Euler problems (#4747) 2021-10-12 00:33:44 +08:00
problem_100 Add Project Euler problem 100 solution 1 (#8175) 2023-03-14 11:09:36 +05:30
problem_101 Get rid of the Union (#6246) 2022-07-11 13:11:17 +02:00
problem_102
problem_104
problem_107
problem_109 Make some ruff fixes (#8154) 2023-03-01 17:23:33 +01:00
problem_112
problem_113
problem_114
problem_115
problem_116
problem_117
problem_119
problem_120 Add Project Euler 120 solution (#2887) 2020-10-06 08:41:15 +05:30
problem_121 feat: Add solution for Project Euler Problem 121 (#4261) 2021-03-20 11:29:48 +05:30
problem_123
problem_125
problem_129
problem_131 Add more ruff rules (#8767) 2023-05-26 09:34:17 +02:00
problem_135
problem_144 The black formatter is no longer beta (#5960) 2022-01-30 20:29:54 +01:00
problem_145
problem_173
problem_174
problem_180 The black formatter is no longer beta (#5960) 2022-01-30 20:29:54 +01:00
problem_187
problem_188
problem_191
problem_203 Make some ruff fixes (#8154) 2023-03-01 17:23:33 +01:00
problem_205
problem_206
problem_207
problem_234
problem_301
problem_493
problem_551 Add Flake8 comprehensions to pre-commit (#7235) 2022-10-15 19:29:42 +02:00
problem_587
problem_686 Fix some typos in solution 1 of euler 686 (#6112) 2022-05-12 11:28:45 +08:00
problem_800 Add Project Euler problem 800 solution 1 (#8567) 2023-04-01 17:48:13 +05:30
__init__.py
README.md Fix broken links to Gitter Community (Fixes: #8197) (#8546) 2023-03-26 18:20:47 +02:00

Project Euler

Problems are taken from https://projecteuler.net/, the Project Euler. Problems are licensed under CC BY-NC-SA 4.0.

Project Euler is a series of challenging mathematical/computer programming problems that require more than just mathematical insights to solve. Project Euler is ideal for mathematicians who are learning to code.

The solutions will be checked by our automated testing on GitHub Actions with the help of this script. The efficiency of your code is also checked. You can view the top 10 slowest solutions on GitHub Actions logs (under slowest 10 durations) and open a pull request to improve those solutions.

Solution Guidelines

Welcome to TheAlgorithms/Python! Before reading the solution guidelines, make sure you read the whole Contributing Guidelines as it won't be repeated in here. If you have any doubt on the guidelines, please feel free to state it clearly in an issue or ask the community in Gitter. You can use the template we have provided below as your starting point but be sure to read the Coding Style part first.

Coding Style

  • Please maintain consistency in project directory and solution file names. Keep the following points in mind:

    • Create a new directory only for the problems which do not exist yet.
    • If you create a new directory, please create an empty __init__.py file inside it as well.
    • Please name the project directory as problem_<problem_number> where problem_number should be filled with 0s so as to occupy 3 digits. Example: problem_001, problem_002, problem_067, problem_145, and so on.
  • Please provide a link to the problem and other references, if used, in the module-level docstring.

  • All imports should come after the module-level docstring.

  • You can have as many helper functions as you want but there should be one main function called solution which should satisfy the conditions as stated below:

    • It should contain positional argument(s) whose default value is the question input. Example: Please take a look at Problem 1 where the question is to Find the sum of all the multiples of 3 or 5 below 1000. In this case the main solution function will be solution(limit: int = 1000).
    • When the solution function is called without any arguments like so: solution(), it should return the answer to the problem.
  • Every function, which includes all the helper functions, if any, and the main solution function, should have doctest in the function docstring along with a brief statement mentioning what the function is about.

    • There should not be a doctest for testing the answer as that is done by our GitHub Actions build using this script. Keeping in mind the above example of Problem 1:
    def solution(limit: int = 1000):
        """
        A brief statement mentioning what the function is about.
    
        You can have a detailed explanation about the solution method in the
        module-level docstring.
    
        >>> solution(1)
        ...
        >>> solution(16)
        ...
        >>> solution(100)
        ...
        """
    

Solution Template

You can use the below template as your starting point but please read the Coding Style first to understand how the template works.

Please change the name of the helper functions accordingly, change the parameter names with a descriptive one, replace the content within [square brackets] (including the brackets) with the appropriate content.

"""
Project Euler Problem [problem number]: [link to the original problem]

... [Entire problem statement] ...

... [Solution explanation - Optional] ...

References [Optional]:
- [Wikipedia link to the topic]
- [Stackoverflow link]
...

"""
import module1
import module2
...

def helper1(arg1: [type hint], arg2: [type hint], ...) -> [Return type hint]:
    """
    A brief statement explaining what the function is about.

    ... A more elaborate description ... [Optional]

    ...
    [Doctest]
    ...

    """
    ...
    # calculations
    ...

    return


# You can have multiple helper functions but the solution function should be
# after all the helper functions ...


def solution(arg1: [type hint], arg2: [type hint], ...) -> [Return type hint]:
    """
    A brief statement mentioning what the function is about.

    You can have a detailed explanation about the solution in the
    module-level docstring.

    ...
    [Doctest as mentioned above]
    ...

    """

    ...
    # calculations
    ...

    return answer


if __name__ == "__main__":
    print(f"{solution() = }")