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
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt

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

- name: End-to-end calculation (1,000 digits)
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
run: |
python verify.py \
--calculated /tmp/pi_1000_calculated.txt \
--reference tests/fixtures/pi_1000_reference.txt
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ venv/
env/

*.log
*.tmp
*.tmp

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

# Downloaded reference file (10 MB) — do not commit.
reference_pi.txt
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,40 @@ pip install -r requirements.txt

## Usage

Run the latest π calculator:
### Recommended calculator: `pi_project_v4.py`

Run the latest π calculator (Chudnovsky + binary splitting + GMPY2):

```bash
python pi_project_v4.py
```

The program calculates π using the Chudnovsky algorithm and saves the result to a text file.
The program prompts for the number of digits, calculates π, verifies it, and saves the result to `pi_<digits>.txt`.

Non-interactive mode (useful for scripting and CI):

```bash
python pi_project_v4.py --digits 1000 --output pi_1000.txt
```

Options:

- `--digits N` number of decimal digits of π to calculate
- `--output FILE` output file (default: `pi_<digits>.txt`)

### Verification

To verify the calculated result:
To verify a calculated result against a reference:

```bash
python verify.py
```

The verification tool compares the calculated digits against a reference and reports the number of matched and mismatched digits.
This downloads a 10,000,000-digit reference and compares it to `pi_10000000.txt`. Use `--reference` to supply a local reference file instead, and `--sha256` to validate the reference's integrity. The expected SHA-256 must be established from a trusted source.

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

### Example

Expand All @@ -98,6 +117,18 @@ Verification time: 0.0474 seconds

The result contains **10,000,000 characters** including the `3` before the decimal point.

## Calculators compared

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

| 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 much slower and heavier RAM than v4 at scale. |
| `pie.py` | Machin-like arctan | Python stdlib (`decimal`) | Small digit counts only | Legacy, O(digits²). Impractical beyond a few thousand digits. |

`pi_v3.py` and `pie.py` are kept for reference and are not covered by the CI performance path.

## Contributing

Contributions are welcome! Please follow these steps:
Expand Down
Loading
Loading