Python-based Project Euler command line tool.
EulerPy is a command line tool designed to streamline the process of solving Project Euler problems using Python. The package focuses on two main tasks: firstly, to create Python “template” files with a docstring containing the text of a Project Euler problem for ease-of-reference, and secondly, to check whether a problem has been solved correctly.
EulerPy can be installed (and updated) from PyPI using pip:
$ pip install --upgrade EulerPy
Conversely, it can be uninstalled using pip as well.
$ pip uninstall EulerPy
First, you’ll want to cd to the directory where your Project Euler files are being stored.
$ mkdir ~/project-euler $ cd ~/project-euler
At this point, you’ll probably want to run the euler command, which will prompt to create 001.py, a file containing the text to Project Euler problem #1 as its docstring.
$ euler No Project Euler files found in the current directory. Generate file for problem #1? [Y/n]: Y Successfully created "001.py". $ cat 001.py """ Project Euler Problem #1 ========================= If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. """
At this point, you can open up your editor of choice and code up a solution to the problem, making sure to print() the output. Once you feel that you’ve solved the problem, run the euler command again to verify your solution is correct. If the answer is correct, the solution will be printed in green and the script will ask to generate the next problem file. If incorrect, the solution will be printed in red instead.
$ euler Checking "001.py" against solution: [no output] # (output in red) $ echo print 42 >> 001.py $ euler Checking "001.py" against solution: 42 # (output in green) Generate file for problem #2? [Y/n]: Y Successfully created "002.py".
EulerPy also comes with five options that act as commands. (The --help option can be used to display a summary of all of the options.)
--cheat / -c
The --cheat option will print the answer to a problem after prompting the user to ensure that they want to see it. If no problem argument is given, it will print the answer to the problem that they are currently working on.
$ euler --cheat View solution to problem #2? [y/N]: Y The solution to problem #2 is <redacted>. $ euler --cheat 100 View solution to problem #100? [y/N]: Y The solution to problem #100 is <redacted>.
--generate / -g
The --generate option will create a Python file for the given problem number. If no problem number is given, it will overwrite the most recent problem with a file containing only the problem docstring (after prompting the user).
$ euler --generate Generate file for problem #2? [Y/n]: Y "002.py" already exists. Overwrite? [y/N]: Successfully created "002.py". $ euler --generate 5 Generate file for problem #5? [Y/n]: n Aborted!
euler <problem> is equivalent to euler --generate <problem> if the file does not exist.
$ cat 005.py cat: 005.py: No such file or directory $ euler 5 Generate file for problem #5? [Y/n]: n Aborted!
--preview / -p
The --preview option will print the text of a given problem to the terminal; if no problem number is given, it will print the current problem instead.
$ euler --preview Project Euler Problem #2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. $ euler --preview 3 Project Euler Problem #3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143?
--skip / -s
The --skip option will prompt the user to “skip” to the next problem.
$ euler --skip Current problem is problem #2. Generate file for problem #3? [y/N]: Y Successfully created "003.py".
--verify / -v
The --verify option will check whether a given problem file outputs the correct solution to the problem. If no problem number is given, it will check the current problem.
$ euler --verify Checking "003.py" against solution: [no output] # (output in red) $ euler --verify 1 Checking "001.py" against solution: <redacted> # (output in green)
euler <problem> is equivalent to euler --verify <problem> if the file does exist.
$ cat 001.py """ Project Euler Problem #1 ========================= ... """ $ euler 1 Checking "001.py" against solution: <redacted>
See this blog post for insight into the development process.
EulerPy is licensed under the MIT License.