Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,34 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
pip install -e .

- name: Run test suite
run: |
python -m pytest

- name: End-to-end calculation (1,000 digits)
- name: Verify committed reference fixture integrity
run: |
python pi_project_v4.py --digits 1000 --output /tmp/pi_1000_calculated.txt
test "$(wc -c < /tmp/pi_1000_calculated.txt)" -eq 1002

- name: End-to-end verification against committed reference
EXPECTED="823a2e34f63c5d5f30a27733976df5a1ab57feaab505f40d95d3dd3fefa425cc"
ACTUAL="$(sha256sum tests/fixtures/pi_1000_reference.txt | awk '{print $1}')"
if [ "$ACTUAL" != "$EXPECTED" ]; then
echo "ERROR: Reference fixture SHA-256 mismatch!"
echo " expected: $EXPECTED"
echo " actual: $ACTUAL"
exit 1
fi
echo "Reference fixture SHA-256 verified: $ACTUAL"

- name: End-to-end calculate + verify against committed reference
run: |
python verify.py \
--calculated /tmp/pi_1000_calculated.txt \
piproject calculate --digits 1000 --output /tmp/pi_1000_calculated.txt
# Verify the calculated output against the INDEPENDENT committed reference.
# This is NOT self-verification: the reference is a separate file with
# documented provenance (see tests/fixtures/pi_1000_reference.provenance.json).
piproject verify \
--input /tmp/pi_1000_calculated.txt \
--reference tests/fixtures/pi_1000_reference.txt

- name: Benchmark (1,000 digits)
run: |
piproject benchmark --digits 1000 --repeats 3
21 changes: 17 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ env/
*.log
*.tmp

# Generated pi output (can be tens of MB) — do not commit.
pi_*.txt
# Generated pi output — do not commit.
/pi_*.txt
reference_pi.txt

# Downloaded reference file (10 MB) — do not commit.
reference_pi.txt
# Build artifacts
/build/
/dist/
/*.egg-info/
/src/*.egg-info/

# Benchmark outputs
/benchmarks/*.json

# Local development
.env
.pytest_cache/
.coverage
htmlcov/
183 changes: 80 additions & 103 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
<a href="https://github.com/zomop/PiProject/actions/workflows/ci.yml">
<img src="https://img.shields.io/github/actions/workflow/status/zomop/PiProject/ci.yml?branch=main&label=CI" alt="CI status"/>
</a>
<a href="https://github.com/zomop/PiProject">
<img src="https://img.shields.io/badge/python-3.11%2B-blue?logo=python" alt="Python 3.11+"/>
</a>
<img src="https://img.shields.io/badge/python-3.9%2B-blue?logo=python" alt="Python 3.9+"/>
<img src="https://img.shields.io/badge/gmpy2-2.3.2-green" alt="gmpy2 2.3.2"/>
<img src="https://img.shields.io/badge/digits-10M%2B%20verified-purple?logo=calculator" alt="10M+ digits verified"/>
<img src="https://img.shields.io/badge/tests-77%20passing-blue" alt="Tests passing"/>
<img src="https://img.shields.io/badge/digits-10M%2B-purple?logo=calculator" alt="10M+ digits supported"/>
</p>

<p align="center">
<b>High-precision π computation in Python</b> · Chudnovsky algorithm · Binary splitting · GMPY2
<b>PiProject</b> — High-precision π computation and independent verification
<br/>Chudnovsky algorithm · Binary splitting · GMPY2 · BBP spot checks
</p>

---

# PiProject

**PiProject** is an open-source Python project for computing **π (pi)** to millions of digits with the
**Chudnovsky algorithm** and **binary splitting**, powered by **GMPY2** arbitrary-precision arithmetic.
It also verifies calculated digits against a reference. The project has calculated and verified
**10,000,000 digits of π with 0 mismatches**.
**PiProject** computes **π (pi)** to millions of digits using the **Chudnovsky
algorithm** with **binary splitting** and **GMPY2** arbitrary-precision
arithmetic, and verifies results through an **independent verification engine**
that never trusts the computation engine.

## The math

Expand All @@ -38,7 +38,7 @@ one of the fastest known methods for computing π:
{(3k)! (k!)^3 \left(640320\right)^{3k + 3/2}}
```

Equivalently,
Equivalently:

```math
\pi = \frac{426880 \sqrt{10005} \, Q}{T}
Expand All @@ -53,8 +53,8 @@ flowchart TD
B --> C["Compute P, Q, T integers"]
C --> D["Integer square root of 10005"]
D --> E["Assemble pi = 426880 * sqrt(10005) * Q / T"]
E --> F["Format digits and guard-digit trim"]
F --> G["Verify against known pi digits"]
E --> F["Format digits & guard-digit trim"]
F --> G["Independent verification"]
G --> H["Save pi_N.txt"]
style A fill:#e2e8f0,stroke:#334155
style H fill:#10b981,stroke:#047857,color:#fff
Expand All @@ -65,33 +65,27 @@ flowchart TD
- 🎯 High-precision calculation of π (millions of digits)
- ⚡ Chudnovsky algorithm + binary splitting
- 🔢 GMPY2 arbitrary-precision integer arithmetic
- 🔍 Digit-by-digit reference verification
- 📄 Automatic output to `pi_<digits>.txt`
- 🧪 Tested & verified (47 tests, CI on every push)
- 🔍 Independent verification engine with explicit statuses
- 🧮 BBP hexadecimal spot checks (algorithmically independent)
- 📄 Streaming output to `pi_<digits>.txt`
- 🖥️ Professional CLI: `piproject calculate/verify/benchmark/inspect/hash`
- 🧪 77 automated tests, CI on every push

## Installation

```bash
git clone https://github.com/zomop/PiProject.git
cd PiProject
pip install -r requirements.txt
pip install -e . # install the `piproject` command
```

## Usage

<details>
<summary><b>▶ Recommended calculator: <code>pi_project_v4.py</code></b> (click to expand)</summary>

Run interactively:
### Calculate

```bash
python pi_project_v4.py
```

Or non-interactively (handy for scripting and CI):

```bash
python pi_project_v4.py --digits 1000 --output pi_1000.txt
piproject calculate --digits 1000000 --output pi_1000000.txt
```

Options:
Expand All @@ -101,115 +95,98 @@ Options:
| `--digits N` | number of decimal digits of π to calculate |
| `--output FILE` | output file (default: `pi_<digits>.txt`) |

</details>

<details>
<summary><b>🔎 Verify against a reference</b> (click to expand)</summary>

```bash
python verify.py
```

This compares `pi_10000000.txt` against a downloaded 10,000,000-digit reference. Use a local file instead:
### Verify

```bash
python verify.py --calculated pi_1000.txt \
--reference tests/fixtures/pi_1000_reference.txt \
--sha256 <expected>
piproject verify --input pi_1000000.txt --reference reference.txt
```

The expected SHA-256 must be established from a trusted source — it is not hard-coded.
Options:

</details>
| Option | Meaning |
|--------|---------|
| `--input FILE` | calculated pi file to verify |
| `--reference FILE` | reference file for full digit-by-digit comparison |
| `--bbp POSITIONS` | BBP spot-check positions (comma-separated, e.g. `1,10,100`) |

**Example output**
### Benchmark

```text
3.14159265358979323846264338327950288419716939937510582097494459230781...
```bash
piproject benchmark --digits 1000000 --repeats 3
```

## Calculators compared

PiProject ships three implementations. Use `pi_project_v4.py` unless you have a specific reason not to.
### Inspect

| File | Algorithm | Dependencies | Best for | Notes |
|------|-----------|--------------|----------|-------|
| `pi_project_v4.py` | Chudnovsky + binary splitting | `gmpy2` | **Recommended**, millions of digits | Fastest, lowest memory at scale. Used by CI. |
| `pi_v3.py` | Chudnovsky + binary splitting | Python stdlib (`decimal`) | Medium digit counts | Pure Python, no external deps, but slower at scale. |
| `pie.py` | Machin-like arctan | Python stdlib (`decimal`) | Small digit counts only | Legacy, O(digits²). Impractical beyond a few thousand digits. |

## Contributing

<details>
<summary><b>👉 How to contribute</b> (click to expand)</summary>

1. Fork the repository.
2. Create a feature branch: `git checkout -b feature/amazing-feature`
3. Commit your changes: `git commit -m "Add amazing feature"`
4. Push to the branch: `git push origin feature/amazing-feature`
5. Open a Pull Request.

You can contribute by improving calculation performance, reducing memory usage,
improving verification, adding features, or improving documentation.

</details>

## FAQ
```bash
piproject inspect --input pi_1000000.txt
```

<details>
<summary><b>❓ How many digits can PiProject calculate?</b></summary>
### Hash

Tested successfully with **10,000,000 digits of π**.
```bash
piproject hash --input pi_1000000.txt
```

</details>
## Verification Statuses

<details>
<summary><b>❓ How is π calculated?</b></summary>
PiProject uses explicit, unambiguous verification statuses:

The **Chudnovsky algorithm** with **binary splitting** and **GMPY2** arbitrary-precision arithmetic.
Each series term yields roughly 14 correct digits.
| Status | Meaning |
|--------|---------|
| `CALCULATED` | Value computed; no verification |
| `STRUCTURE_VALID` | Passes format/length/digit checks |
| `PREFIX_VERIFIED` | Matches known reference for checked prefix only |
| `FULLY_VERIFIED` | Every digit compared against a trustworthy reference |
| `INDEPENDENTLY_VERIFIED` | Confirmed by an independent algorithm (BBP) |
| `VERIFICATION_FAILED` | A mismatch or error was detected |

</details>
**Never** `FULLY_VERIFIED` from a prefix check alone.

<details>
<summary><b>❓ Is the 10-million-digit result verified?</b></summary>
## Verification Coverage

Yes. Compared against a reference of 10,000,000 characters —
**10,000,000 matches, 0 mismatches**.
| Digits | Authoritative Check | Status |
|--------|-------------------|--------|
| ≤ 1,000 | All digits | `FULLY_VERIFIED` (built-in reference) |
| > 1,000 | First 1,000 digits | `PREFIX_VERIFIED` (built-in) |
| Any | All digits vs. external reference | `FULLY_VERIFIED` (with `--reference`) |

</details>
The built-in reference covers the first 1,000 decimal digits. For full
verification at scale, provide an external reference file with `--reference`.

<details>
<summary><b>❓ Can I modify the project?</b></summary>
## Verified Results

Yes. PiProject is open source and may be studied, modified, and improved
according to the repository's license (see below).
Independently cross-checked against decimal/Machin computation and the
IonicaBizau/pi-decimals reference:

</details>
| Digits | SHA-256 (decimal digits) | Method |
|--------|-------------------------|--------|
| 1,000 | `808b01bd…2292bd` | decimal/Machin + Chudnovsky match |
| 10,000 | `7406a2be…507ab5` | decimal/Machin + Chudnovsky match |
| 100,000 | `5ebe8007…482bc2` | decimal/Machin + Chudnovsky match |
| 1,000,000 | `7806ee47…bbf7069` | IonicaBizau/pi-decimals reference match |

## Roadmap / ideas
## Documentation

- Add more decimal-places benchmarks
- Parallelize binary splitting across CPU cores
- Add a web demo / WASM build
- More digit visualizations like `assets/pi-random-walk.svg`
- [`docs/architecture.md`](docs/architecture.md) — System architecture
- [`docs/algorithms.md`](docs/algorithms.md) — Chudnovsky, binary splitting, BBP
- [`docs/verification.md`](docs/verification.md) — Verification methodology
- [`docs/reproducibility.md`](docs/reproducibility.md) — Benchmarks & SHA-256 fingerprints

## Acknowledgments
## Contributing

- **GMPY2** — High-performance arbitrary-precision arithmetic
- **Chudnovsky Brothers** — Chudnovsky algorithm for calculating π
- **Python** — Programming language used for the project
- Reference data used for verification
1. Fork the repository
2. Create a feature branch: `git checkout -b feature/amazing-feature`
3. Commit your changes: `git commit -m "Add amazing feature"`
4. Push to the branch: `git push origin feature/amazing-feature`
5. Open a Pull Request

## License

This project is open source. A `LICENSE` file has not yet been added —
the license terms are pending the repository owner's decision
(no MIT or other license was selected without authorization).
the license terms are pending the repository owner's decision.

---

<p align="center">
Made with ❤️ by <b>[zomop](https://github.com/zomop)</b> ·
September 8, 2026
Made with ❤️ by <b>[zomop](https://github.com/zomop)</b>
</p>
52 changes: 52 additions & 0 deletions docs/algorithms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Algorithms

## Chudnovsky Algorithm

The primary computation engine uses the Chudnovsky formula:

```
1/pi = 12 * sum_{k=0}^{inf} (-1)^k * (6k)! * (545140134*k + 13591409) /
((3k)! * (k!)^3 * (640320)^(3k + 3/2))
```

Equivalently:

```
pi = 426880 * sqrt(10005) * Q / T
```

where `P`, `Q`, `T` are computed via binary splitting over the series term
ratio. Each term yields approximately 14.18 correct decimal digits.

## Binary Splitting

Binary splitting recursively divides the series range [a, b) into halves,
computing (P, Q, T) triplets for each half and combining them:

```
P = P1 * P2
Q = Q1 * Q2
T = T1 * Q2 + P1 * T2
```

This reduces the number of large-integer multiplications compared to naive
term-by-term summation.

## BBP Formula (Independent Spot Checks)

The Bailey–Borwein–Plouffe formula computes hexadecimal digits of pi at
arbitrary positions without computing preceding digits:

```
pi = sum_{k=0}^{inf} 1/16^k * (4/(8k+1) - 2/(8k+4) - 1/(8k+5) - 1/(8k+6))
```

This is algorithmically independent of Chudnovsky and is used for spot-checking
individual positions, not full digit-range verification.

## Precision Management

- Guard digits (default: 20) are computed beyond the requested count
- Truncation error is confined to the discarded guard digits
- The result is truncated (not rounded) to exactly N fractional digits
- gmpy2 global precision is never mutated
Loading
Loading