Skip to content

dwchal/picalc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pi Calculator

A Python program that computes π (pi) one digit at a time and outputs each digit as it's computed.

Description

This project implements two different algorithms to calculate the digits of π one at a time:

  1. Spigot Algorithm (pi_calculator.py): An unbounded spigot algorithm that generates digits of π sequentially
  2. Nilakantha Series (pi_calculator_bbp.py): A simplified implementation that demonstrates the concept of generating π digits one at a time

Both implementations:

  • Compute π digit by digit
  • Display each digit as it's calculated
  • Format the output for readability
  • Can be interrupted at any time with Ctrl+C

Requirements

  • Python 3.6 or higher

Usage

Run either implementation with:

# Spigot Algorithm (more accurate)
python pi_calculator.py

# Nilakantha Series (simplified demonstration)
python pi_calculator_bbp.py

By default, the Spigot implementation will compute 1000 digits, while the Nilakantha implementation will compute 100 digits. You can modify these by changing the num_digits variable in the respective main() functions.

How It Works

Spigot Algorithm

The Spigot algorithm is an unbounded algorithm that can generate an arbitrary number of digits of π. It works by:

  1. Maintaining a set of state variables
  2. Performing transformations on these variables
  3. Extracting one digit of π with each iteration

The algorithm is memory-efficient and doesn't require storing all previously computed digits.

Nilakantha Series

The implementation in pi_calculator_bbp.py uses a combination of:

  1. Pre-computed digits for the first few decimal places
  2. A simple pattern generator for additional digits (for demonstration purposes)

In a real-world application, you would use a more sophisticated algorithm like the Bailey-Borwein-Plouffe (BBP) formula or continue the Nilakantha series with higher precision.

Example Output

Computing π one digit at a time:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679...

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages