Aryabhata's digit‑pair square‑root algorithm
Project description
Aryabhata
A Python implementation of Aryabhata’s digit‑pair square‑root algorithm — a classical Indian arithmetic method for extracting square roots using only integer operations.
This project provides:
- A mathematically exact square‑root engine
- A modern CLI
- Optional ASCII animation of the digit‑pair extraction
- 100% unit‑test and branch coverage
Features
- Exact integer arithmetic — no floating point drift
- Arbitrary precision via digit‑pair extension
- Decimal output with proper zero‑padding
- Debug mode exposing the invariant:
N = root² + remainder - Optional ASCII animation showing every digit trial
- Pytest test suite with full coverage
Installation
Install with pip:
pip install aryabhata
Local development:
git clone https://github.com/djstompzone/aryabhata
cd aryabhata
pip install -e .[dev]
Usage
Basic
python -m aryabhata 82
Or
aryabhata 82
Output:
9
With decimal precision
aryabhata 82 --digits 3
Output:
9.055
Debug mode
aryabhata 82 --digits 3 --debug
Output:
9.055
[scaled-root] 9055
[remainder] 6975
[identity] 8200000 = 9055^2 + 6975
ASCII animation
aryabhata 82 --digits 3 --animate
This plays a frame‑by‑frame digit‑pair square‑root extraction in your terminal.
Control speed with:
--fps 15
How the algorithm works
Aryabhata’s method operates by grouping digits into base‑100 pairs and extracting square‑root digits one at a time using this rule:
At each step:
Choose max x such that (20R + x)·x ≤ remainder
R := 10R + x
remainder := remainder − (20R + x)·x
Where R is the root built so far.
This is mathematically equivalent to long‑hand square‑root extraction but works entirely in integers.
Library API
from aryabhata.sqrt import sqrt_aryabhata
root, remainder = sqrt_aryabhata(82, digits=3)
Returns:
root = floor( sqrt(n) × 10^digits )
remainder = n × 10^(2·digits) − root²
Testing
Run all tests with coverage:
poetry run coverage run -m pytest
poetry run coverage report -m
All code paths are fully covered.
Why this exists
Most square‑root implementations hide the math behind floating point hardware.
This project exposes the actual arithmetic behind root extraction — the same logic used by scribes, calculators, and engineers long before digital computers existed.
It’s math you can watch happening.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aryabhata-1.0.0.tar.gz.
File metadata
- Download URL: aryabhata-1.0.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.9 Linux/6.2.1-PRoot-Distro
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41a264be023f77a1ea97f42a851b40e2de01a9a607a4389d8ad5893258ddd0dc
|
|
| MD5 |
62a94e57079e73e3021517e596e3d17f
|
|
| BLAKE2b-256 |
662e83bb797fbb722f7eb3dd293373fe2b084c32f699114fa338c2f2fa89e839
|
File details
Details for the file aryabhata-1.0.0-py3-none-any.whl.
File metadata
- Download URL: aryabhata-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.9 Linux/6.2.1-PRoot-Distro
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2246da6b1ce3ae29b974f354922b3c5591c0850ed95e9856c17db7ce7db8eaa
|
|
| MD5 |
6513373e966158451cbab61d93f16e68
|
|
| BLAKE2b-256 |
d733d81b0007314591edc8f5fcdbf0119b1a0dae73f3f2a9ffd05ab56cb33661
|