Implementing the Spigot algorithm for `π` pi
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The spigot algorithm is interesting because it emits digits of π one at a time. Instead of repeatedly refining a floating-point approximation, it keeps an integer state and “drips” out decimal digits in sequence.
That makes it a good teaching algorithm. It is not the fastest way to compute millions of digits, but it is compact, deterministic, and useful for learning how digit-extraction algorithms work.
Why It Is Called a Spigot Algorithm
A spigot algorithm produces output incrementally, like water coming from a tap. After enough internal updates, one more digit becomes safe to print without changing later. That is the key distinction from methods that compute a big approximation and only then format the result.
For π, the classic decimal spigot algorithm uses an integer array and repeated carry propagation. The state array stores partial remainders, and each outer iteration pushes the system one step closer to the next stable digit.
A Runnable Python Implementation
The following implementation generates decimal digits as a string. It uses only integer arithmetic.
For a short run, this prints 3.14159265358979. The implementation avoids floating-point rounding issues because every step stays in integer space.
How the Carry Logic Works
The hardest part is not the array update itself. It is the logic around predigit, 9, and 10.
Sometimes the next extracted value is definitely safe, so you can append it immediately. Sometimes it is 9, which means the previous digit might still need to be adjusted later. Sometimes it becomes 10, which means a carry ripples backward and turns any held 9 digits into 0 digits.
That is why the code keeps:
- '
predigitfor the most recent tentative digit' - '
held_digitsfor a run of pending9values' - '
resultfor finalized digits'
Without that bookkeeping, you will occasionally emit incorrect digits near carry boundaries.
Performance Characteristics
This approach is educational, but it is not the best algorithm for high-performance arbitrary-precision work. Modern π computations often use formulas such as Chudnovsky with fast multiplication libraries.
The spigot method remains useful when you want:
- a digit-by-digit generator
- a small integer-only implementation
- an algorithm that is easy to trace by hand on small examples
Space usage grows with the number of digits requested because the remainder array must be large enough to support the extraction process.
When to Use It
Use the spigot algorithm in classroom material, coding interviews about number generation, or toy projects that print the first few hundred digits. If your real goal is numerical analysis or serious high-precision computation, choose a faster algorithm and a big-number library.
The important lesson is algorithmic shape, not just the constant factor. Spigot-style digit generation shows that there are cases where output can be streamed one symbol at a time while preserving correctness.
Common Pitfalls
- Using floating-point arithmetic. That defeats the purpose and introduces rounding errors into a digit-extraction algorithm.
- Emitting
9digits too early. They may need to change if a later carry produces10. - Forgetting the final pending digit. The last
predigitstill has to be appended after the loop. - Assuming this is the fastest
πalgorithm available. It is mainly valuable for clarity and incremental output. - Choosing an array that is too small. The working state must be sized relative to the number of digits requested.
Summary
- The spigot algorithm generates digits of
πincrementally. - A practical implementation uses integer arithmetic and a remainder array.
- Correct handling of tentative
9and10cases is the core detail. - The algorithm is ideal for learning and small demonstrations.
- For very large computations, faster high-precision methods are usually a better choice.
Related reading
- Impossible-to-find bug in a program that equalizes wealth in a group UVA 10137, The Trip
- Improve algorithmic thinking
- Improving performance of click detection on a staggered column isometric grid
- Improving search result using Levenshtein distance in Java
- in-place permutation of a array follows this rule
- In-place transposition of a matrix
- In-order iterator for binary tree
- In-place array reordering?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.