Project Euler in Python
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
This repository contains a collection of hand-crafted solutions to these problems in the Python 3 programming language, using only built-in functions and modules from the Python Standard Library. There are no further dependencies.
The various solutions all contain a main() function that computes the solution, plus potentially a set of helper
functions that performs general tasks. The latter are often imported and re-used in solutions to later problems. These
may also be useful to you; for an overview of these, see the table under Execution / Usage.
Table of Contents
Assuming you have Python 3.x installed, simply clone the repository from a terminal, and you are ready to go:
git clone https://github.com/landsgevaer/euler.pyTo run the main script, fire up a terminal window and run the following command:
python3 main.pyThis will run all available solutions to solved problems, verify their correctness, measure their runtime, and show an
overview of the results in the terminal. The results will also be written to a file results.md (you may specify a
different filename by providing it as a command-line argument, like python3 main.py my_output.md).
README.md file.
You may also run a single solution separately using a command like:
python3 solutions/problem0000.pyThe problems are numbered according to the problem ID on the Problem Archives. Problem 0 refers to the problem posed when registering (note that the numbers in problem 0 are randomised for each visitor).
The following helper functions are provided in the various solutions:
| Module | Function | Description |
|---|---|---|
| problem0002 | generate_fibonacci() | Generator of Fibonacci numbers. |
| problem0003 | factorize_primes(n) | Decompose a number into prime factors with their exponents. |
| problem0003 | generate_primes() | Generator of prime numbers. |
| problem0003 | is_prime(number) | Determine whether a number is prime. |
| problem0009 | list_divisors(n) | List all divisors of n. |
| problem0014 | generate_collatz(n) | Generator of Collatz sequence. |
| problem0017 | speak_number(n, *, scale=0) | Returns the English spoken form of a number (up to 10^21) as a string. |
| problem0031 | count_partitions(n, *, allowed=None) | Count how many ways an integer n can be partitioned into numbers from a set. |
| problem0040 | generate_champernowne() | Generator of champernowne constant decimals. |
Because this is a personal project to challenge myself to solve all problems in the Euler Project, no pull requests will be accepted. You are of course free to be inspired by this repository and improve on its solutions yourself. In case of questions or comments on the solutions, feel free to contact me (see the author details below).
Dave R.M. Langers – @landsgevaer – dave@langers.nl
"Project Euler in Python" is distributed under the MIT-license. See LICENSE for more details.