A brute force primality testing program written in Python. It uses a 6k +/- 1 algorithm, that is parallelized using either the CPU or GPU (depending on what's available/selected).
python>=3.8,<3.13pip- CUDA (optional, for GPU acceleration)
-
Clone this repository, and change directory.
$ git clone https://github.com/ralgar/prime9k.git $ cd prime9k -
Create and activate a virtual environment.
$ python -m venv venv $ source venv/bin/activate -
Install the Python package.
$ pip install .
-
Invoke the help dialog to learn how to use the program.
$ prime9k --help usage: prime9k [-h] [-d {cpu,gpu}] [-v] INTEGER Primality Tester 9000 positional arguments: INTEGER a single integer to test (max value: 9223372036854775807) options: -h, --help show this help message and exit -d {cpu,gpu}, --device {cpu,gpu} override selected compute device -v, --verbose show more detailed logs
If you don't want to mess with venvs and dependencies, a single-command Docker Compose build is provided.
- Docker
- NVIDIA Container Toolkit (optional, for GPU acceleration)
-
Build and start the container using Docker Compose.
$ docker compose --profile [cpu,gpu] up
-
Once the program has finished execution, bring the compose stack down.
$ docker compose --profile [cpu,gpu] down
Prime9k comes with a dynamic testing framework that leverages a well-known algebra library (SymPy) to generate valid prime and composite numbers at runtime. A wide range of values are tested (over 20,000 combined prime and composite numbers), and both the CPU and GPU compute algorithms are tested.
After following the Manual Installation steps:
-
Install the
testoptional dependencies.$ pip install .[test]
-
Invoke
pytestfrom the project root.$ pytest
Copyright: (c) 2025, Ryan Algar (ralgar/prime9k)
BSD 2-clause License (see LICENSE or BSD 2-clause)