Skip to main content

Fast number theory utilities backed by Rust

Project description

numbertoolkit

Fast number theory utilities for Python, backed by Rust.

import numbertoolkit

numbertoolkit.pi_digits(10)
# '3.141592653'

Functions

pi_digits(n: int) -> str

Returns the first n significant digits of pi as a string, e.g. pi_digits(5) == "3.1415". Digits are truncated, not rounded.

Implemented with the Chudnovsky series using binary splitting and exact integer arithmetic (no floating point), so it stays fast well into the millions of digits.

Demo

An interactive marimo notebook demoing the package:

uv run marimo edit demo.py

Benchmarks

Compare against mpmath and a stdlib-decimal Machin baseline:

uv run --group bench benchmarks/bench_pi.py

On an Apple Silicon laptop:

digits numbertoolkit mpmath speedup vs mpmath
10,000 0.0008s 0.0052s 6x
100,000 0.0154s 0.1926s 13x
1,000,000 0.2526s 9.0297s 36x

Development

Requires Rust and uv.

make install                   # uv sync: build the extension and set up .venv
make test                      # cargo test + pytest
make bench                     # run the benchmark suite
uvx maturin develop --uv -r    # fast rebuild loop (release mode)

Note: maturin develop without -r produces a debug build that is 10–50x slower — always use release mode when timing anything.

Releasing

Tests run automatically on GitHub (.github/workflows/ci.yml); publishing to PyPI is done manually. Thanks to abi3, each wheel covers Python 3.10+ on its platform, and cross-compiling doesn't need a target Python interpreter.

make wheels builds the full release set (macOS wheel, both Linux wheels via zig, sdist) and make pypi runs the tests, builds, and uploads. The underlying commands:

# wheel for this machine + source distribution (lands in target/wheels/)
uvx maturin build --release
uvx maturin sdist

# optional: manylinux wheels cross-compiled from macOS via zig
rustup target add x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu
uvx --with ziglang maturin build --release --zig --target x86_64-unknown-linux-gnu
uvx --with ziglang maturin build --release --zig --target aarch64-unknown-linux-gnu

# upload everything (needs a PyPI API token, e.g. via UV_PUBLISH_TOKEN)
uv publish target/wheels/*

Bump the version in pyproject.toml before building. Platforms without an uploaded wheel (e.g. Windows) fall back to the sdist, which requires a Rust toolchain to install.

License

This project is MIT licensed. It statically links malachite (LGPL-3.0-only) for big-integer arithmetic; malachite's source is available on crates.io. If you embed this library in a closed-source distribution, the LGPL relinking requirements apply to the bundled malachite code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numbertoolkit-0.1.0.tar.gz (68.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (774.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (617.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

numbertoolkit-0.1.0-cp310-abi3-macosx_11_0_arm64.whl (610.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file numbertoolkit-0.1.0.tar.gz.

File metadata

  • Download URL: numbertoolkit-0.1.0.tar.gz
  • Upload date:
  • Size: 68.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for numbertoolkit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 af03a324f5a6e055daa5fad3c5837ef63cc1e56319fe7d788111caa65501febb
MD5 0204f191b2641181a6b5050b53ab52c0
BLAKE2b-256 238f48748c5cfc59e387b7177842ba02e8a421304d6244264a9d4c66f9816d56

See more details on using hashes here.

File details

Details for the file numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 774.0 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c3159bfcb72b3bff38556fb53d69d980aa65d78c790e787c15711561564b72f0
MD5 e1f373bf06a30b6cb694434c7e4f6daa
BLAKE2b-256 58bc7389a28dc60e5092d36ba348a3bf19b254f45018570cb33d008acc2f02dc

See more details on using hashes here.

File details

Details for the file numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

  • Download URL: numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 617.5 kB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for numbertoolkit-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 219af8b1ee2c96b539ea4955559c4e583ea359ba8e596d0a5823ba088caa9617
MD5 98026d661dc2bb4eeb638bb982592b9b
BLAKE2b-256 2e7bc4b6e8e44bf7fe9511ce80f7cdea484db2eeef2a4032d540d712aade331e

See more details on using hashes here.

File details

Details for the file numbertoolkit-0.1.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: numbertoolkit-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 610.2 kB
  • Tags: CPython 3.10+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for numbertoolkit-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3f361a03eed8a195e8f7e5c0119972ae78935d38f643950294f645f6b063eade
MD5 0f6dc579dda71216a0cfd5ba301d9e80
BLAKE2b-256 75ba34cbeaa0b1bdebaf0a21f2758010565fdcca7c29f2d1dfdd3269236aa9b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page