← Back to Calculator
Project 1 of 100 Python Live Projects

π Digit Calculator

This project calculates π to the Nth decimal place using Python. It started as a raw CLI math experiment and later evolved into a small FastAPI website with a cleaner user interface. The core idea was simple but important: do not just print a number — understand how different methods behave, why some fail, and why high precision arithmetic matters when working with real mathematical computation.

Main Goal
Find π to the Nth digit
Core Focus
Precision + algorithms + web integration
Implementation Style
CLI first, then FastAPI website

What this project contains

This project has two usable forms. The first is a raw Python CLI version for direct algorithm practice. The second is a website version built with FastAPI so the same logic can be used through a browser interface.

CLI version

If you only want the raw Python logic, go to sample.py in the repository. That file focuses on the mathematical side of the project without website layers.

Website version

The website version takes user input from an HTML form, sends it to the FastAPI backend, runs the Chudnovsky algorithm in Python, and returns the computed π digits to the browser using Jinja templating.

Stack used

Python 3.14.0
FastAPI 0.135.1

Used for the website version. Flask could also be used, but FastAPI was chosen here for practice.

HTML
Tailwind CSS via browser CDN

Optional. Normal CSS would also work. Tailwind was used for fast styling.

Poetry

Used for environment and dependency practice. A normal venv setup is also completely fine.

Vercel

Used for deployment of the web interface.

How the code journey went

The project was not built by jumping straight into the final solution. Three different approaches were tried in the CLI version. That matters because this project is not only about getting an answer — it is about understanding why some methods are limited and why the final method is worth using.

Method 1

Bailey–Borwein–Plouffe formula

$$\pi = \sum_{k=0}^{\infty} \left[ \frac{1}{16^k} \left( \frac{4}{8k+1} - \frac{2}{8k+4} - \frac{1}{8k+5} - \frac{1}{8k+6} \right) \right]$$

This was one of the first methods attempted in sample.py.

The idea was to take input n, run a loop, apply the BBP formula step by step, and sum the values.

The rough thinking was:

  • take user input n
  • loop through terms
  • apply the formula in each iteration
  • sum all partial values

The problem was that in this implementation it was not giving the level of precision we wanted. It effectively hit a wall around the same rough region where built-in floating point approaches also become disappointing for serious high-digit work.

Verdict: mathematically interesting, but not strong enough for the goal of deep high precision in this project.

Method 2

Using Python’s built-in math.pi

f"{math.pi:.{n}f}"

This method is simple and useful for quick formatting practice. You get math.pi from Python’s math module and slice or format it to the required number of decimals.

That sounds neat until reality lands a slap.

math.pi is still based on floating point precision. It is not designed for huge decimal expansion. So even if the formatting looks dynamic, the underlying value is still limited.

In other words, this method can display digits nicely, but it cannot magically create more true precision than Python float already has.

Verdict: convenient, but limited. Fine for small display tasks, not for real high-precision π calculation.

Method 3

Chudnovsky algorithm

$$\frac{1}{\pi} = 12 \sum_{k=0}^{\infty} \frac{(-1)^k (6k)! (545140134k + 13591409)}{(3k)! (k!)^3 (640320)^{3k + 3/2}}$$

This is the method that solved the real problem.

The Chudnovsky algorithm is famous because it converges very fast. Each iteration contributes roughly 14.18 correct digits of π. That makes it far more practical than the earlier attempts for this project.

Since normal int, float, or standard floating point storage cannot safely handle this kind of precision, we need a high precision arithmetic approach.

Two common choices are:

  • gmpy
  • Decimal

In this project, Decimal was used because it is easier to understand and already available in Python’s standard library.

Verdict: this became the final method because it is fast, precise, and actually suitable for the goal.

Why Decimal is required

getcontext().prec = n + 20

Precision means how many significant digits the Decimal system is allowed to keep during calculation.

This matters because if the internal precision is too low, the result starts rounding too early and the output becomes wrong or padded with garbage digits.

In this project, the precision is set to n + 20 as a safety buffer. The extra digits give the algorithm room to work before the final output is trimmed down to exactly the user-requested size.

That little buffer is not decoration. It is the difference between real precision and fake confidence.

Why factorial is used

from math import factorial

The Chudnovsky formula contains factorial-heavy terms such as:

  • $(6k)!$
  • $(3k)!$
  • $(k!)^3$

Python’s built-in math.factorial is perfect for this part, because it is direct, readable, and reliable for integer factorial computation.

So instead of reinventing factorial logic like a maniac before breakfast, the project simply uses the standard library.

Algorithm flow in plain language

Step 1

Take input

The user enters n, meaning how many decimal places of π are needed.

Step 2

Set precision

Decimal precision is raised to n + 20 so internal calculations stay accurate.

Step 3

Choose iteration count

Since each loop gives around 14.18 digits, the number of loop iterations can be estimated from n.

Step 4

Compute and trim

The formula is applied, the result is computed, and then it is formatted or trimmed to exactly the requested decimal length.

Iteration logic intuition

Because each iteration gives about 14.18 digits, there is no need to loop blindly forever. The program estimates how many terms are needed based on the requested precision. That keeps the algorithm efficient and avoids useless work.

The rough idea is:

iteration_count ≈ (n // 14) + 1

It is not a mystical sacred number falling from the sky. It is just practical estimation based on how quickly Chudnovsky converges.

Website integration with FastAPI

After proving the algorithm in the terminal, the project was expanded into a small web app using FastAPI.

The browser side contains a form where the user enters the desired number of digits.

That value is sent to a FastAPI route using a POST request. The backend reads the form value, runs the calculation function, and then renders the result back to the template.

User → HTML form → FastAPI route → Python algorithm → template response

This made the project more than just a terminal script. It turned it into a small usable product.

About this About page

This page exists for the same reason a good README exists: code alone is not enough.

A proper project should explain:

  • what the project does
  • which methods were tried
  • which problems appeared
  • why the final solution was chosen
  • how the web implementation connects to the core logic

That is what this page is doing. It is basically the project story, the reasoning, and the technical explanation placed directly inside the app.

Caution

Requesting too many digits can put serious pressure on the CPU. High precision math is not free. The more digits requested, the more expensive the computation becomes. So yes, π is beautiful, but it can absolutely bully weak hardware if you let users go wild.

Project summary

This project is a practical exploration of mathematical precision in Python.

It began with simple and limited methods, moved through experimentation, and ended with a proper high precision implementation using the Chudnovsky algorithm and Decimal arithmetic.

It also demonstrates how a pure CLI math program can be upgraded into a web application with FastAPI, HTML templating, styling, and deployment.

So this is not just “a π calculator.” It is a small but meaningful project about algorithm choice, numerical limits, precision handling, backend integration, and clear technical explanation.

Back to Calculator

Powered by Python, precision, and a little mathematical stubbornness.