Skip to main content

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

Clone and install with Poetry:

git clone https://github.com/yourname/aryabhata
cd aryabhata
poetry install

Or install locally:

pip install .

Usage

Basic

python -m aryabhata 82

Output:

9

With decimal precision

python -m aryabhata 82 --digits 3

Output:

9.055

Debug mode

python -m aryabhata 82 --digits 3 --debug

Output:

9.055
[scaled-root] 9055
[remainder]   6975
[identity]    8200000 = 9055^2 + 6975

ASCII animation

python -m 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

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

aryabhata-0.1.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

aryabhata-0.1.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file aryabhata-0.1.2.tar.gz.

File metadata

  • Download URL: aryabhata-0.1.2.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

Hashes for aryabhata-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e620c42b11bcd88768a25e67c70577ccf592c3b565026aee960c59f251dfb969
MD5 92eb3e1694ba76fd0bc90b8981ef60ca
BLAKE2b-256 6926547b5dbfe32df8f0ff3883f293d382e7cddf7462a341d0783e66cd409051

See more details on using hashes here.

File details

Details for the file aryabhata-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aryabhata-0.1.2-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

Hashes for aryabhata-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 69a50cbebecb8c212410f6f1bcbe79d8a120e75d29dd077fd752203c8694ad37
MD5 9be3be2d67f955d893f8a876fb4dec66
BLAKE2b-256 7fde91ad1de2dc888645306726c69219f82390ebfef79323a226095125192614

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