Skip to content

zachliu/gmp_pi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gmp_pi

Computing digits of pi using the Chudnovsky algorithm with GMP (GNU Multiple Precision Arithmetic Library).

Based on code by Hanhong Xue (2002), modified by Torbjorn Granlund (2005).

Prerequisites

This project requires the GMP development library. On Debian/Ubuntu/Linux Mint:

sudo apt install libgmp-dev

The GMP runtime (libgmp10) is typically already installed, but the development headers are not.

Build

make

Usage

# compute 100,000 digits of pi (default)
make run

# compute a custom number of digits
make run DIGITS=1000000

# find a date string in the digits of pi (requires ripgrep)
make find_bday

Memory usage

While the Chudnovsky algorithm is CPU-intensive, it is also surprisingly memory-hungry. The binary splitting approach builds up massive integers at each recursion level, and the intermediate products are stored across multiple stacks (pstack, qstack, gstack). For large digit counts, memory scales roughly linearly — computing 1 billion digits can take several GB of RAM. It's easy to request more digits than your machine has memory for, at which point the OOM killer will silently terminate the process.

To profile heap memory usage, use Valgrind's Massif tool:

sudo apt install valgrind

# profile a run
valgrind --tool=massif ./gmp-chudnovsky 100000

# view the results (produces an ASCII chart of heap usage over time)
ms_print massif.out.<pid>

Why C?

The original implementation is in C because GMP itself is a C library, and this code was written by GMP contributors. C++ would offer no performance advantage here since the bottleneck is GMP's big-number arithmetic, which is hand-tuned C and assembly regardless of the calling language.

About

Computing billions of π digits using GMP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors