Skip to content

Language Shootout

A user-supported site Fastest, Shortest, Simplest

Benchmarks

Pi Digits

Background

MathWorld: Pi Digits.

Variance

Some language implementations have arbitrary precision arithmetic built-in; some provide an arbitrary precision arithmetic library; some use a third-party library (GMP); some provide built-in arbitrary precision arithmetic by wrapping a third-party library.

The work

The work is to use arbitrary precision arithmetic and the same step-by-step algorithm to generate digits of Pi. Do both extract(3) and extract(4). Don't optimize away the work.

How to implement

We ask that contributed programs not only give the correct result, but also use the same algorithm to calculate that result.

Each program should:

  • Calculate the first N digits of Pi
  • Print the digits 10-to-a-line, with the running total of digits calculated

Verification: Use diff to compare program output N=30 with the reference output.

Use a larger command line argument (10000) to check program performance.

Adapt the step-by-step algorithm given on pages 4, 6 & 7 of "Unbounded Spigot Algorithms for the Digits of Pi". (Not the deliberately obscure version given on page 2. Not the Rabinowitz-Wagon algorithm.)

Times are wall-clock milliseconds, with this implementation’s hello-world startup time subtracted. gz is the source in bytes with comments removed and gzipped. style is the idiomatic-code score. Click a heading to sort.

# source ms cpu ms mem KB gz style by
1 Java Temurin #1 4,447.7 5,012.4 487,020 855 ★★★★☆ sysop-