A Pi API - Happy Pi Day logo

Happy Pi Day!

All records are made to be broken, and Emma Haruka Iwao’s 2019 record of 31.4 trillion digits was broken with 50 trillion digits calculated by Timothy Mullican in January 2020. Congratulations, Timothy! He generously sent us a copy of the results and we are publishing his work here in celebration of Pi Day 2020.

The Pi API is now serving all 50 trillion digits! You can try one of the demos on this page or download the files directly from http://storage.googleapis.com/pi50t/index.html (caution: it’s 40 TB!).

Watch a discussion with Emma and Timothy on their achievements and how they did it.

Listen

What does Pi sound like? Each Pi digit is converted to a musical note so you can get to know Pi via music! With 50 trillion digits and playing at 314BPM, it’ll play for 302 758.898 years!

Loading sound samples. Please wait...

Listen (Bach Doodle)

On March 21, 2019, Google presented Bach Doodle to celebrate world renowned German composer and musician Johann Sebastian Bach. You can create a melody line and use the AI to fill in the harmony.

This demo uses Pi digits to create the melody, and then you can uses Google Bach Doodle ML model (Coconet) to infill the harmony. Coconet Javascript library is available as part of Magenta.JS. Learn more with Coconet demo and Coconet demo source.

Listen (Piano Genie)

Piano Genie is an neural network model that allows you to play a note from 1 to 8, and intelligently map it to a great sounding piano performance. In this demo, we map each digit (0 to 9) to (0 to 7) using modulus, and use Piano Genie to map to the next logical note for great sounding Pi.

Loading sound samples. Please wait...

Visualize

Visualize transitions from one digit to the next in Pi using D3.js. Each transition draws a line from a digit in the circle to another digit. Once you have a large amount of digits you can see which digits transition more often and which digits are more common.

Art

For Google Cloud Next 2019 in San Francisco, a team created the Pi Experiment Showcase, and uses the Pi API & generative algorithm to generate unique art pieces for each of the 31.4 trillion digits of Pi.

Here is an art piece to the first 11 digits of e (2.7182818284): 27,182,818,284th digits of Pi

Learn more about the world record & generate your own unique art piece from the Pi Experiment Showcase.

/v1/pi

Get Pi Digits

Returns digits of Pi based on the start digit position and numberOfDigits.

There is no SLA on this service. We may require an API key in the future & or turn off the service. If you really want to use it, let us know!

$ curl 'https://api.pi.delivery/v1/pi?start=0&numberOfDigits=100'
$.get("https://api.pi.delivery/v1/pi", {
  start: 0,
  numberOfDigits: 100
}, function(data) {
  console.log(data);
});
<script src="https://api.pi.delivery/js/pi.js"></script>
<script>
var pi = new Pi();
pi.get(0, 100, function(digits) {
  alert(digits);
});
</script>
<script src="https://api.pi.delivery/js/pi.js"></script>
<script>
var stream = new PiStream();
stream.listen(function(e) {
  console.log(e.detail.position);
  console.log(e.detail.digit);
});
stream.start();
</script>

Fetching Digits

This demo uses the REST API to fetch 100 digits of Pi from the specified starting point.

Press 'Fetch'
var pi = new Pi();
pi.get(0, 100, function(digits) {
  ...
});
{
  "start": 0,
  "numberOfDigits": 100
}
{
  "content": 314159....
}

Streaming Digit

This demo uses the streaming API to stream digits into the page.

var streamer = new PiStream();
streamer.listen(function(e) {
  // e.detail.position
  // e.detail.digit
});
// optional (delay in milliseconds):
// streamer.delay(314);
streamer.start();

Calculating Pi

In 2019, Emma Haruka Iwao broke the Pi digits world record by computing 31,415,926,535,897 digits of Pi using Google Cloud Platform! Learn more details from Emma’s Calculating a record-breaking 31.4 trillion digits blog, and Alex Yee (creator of y-cruncher)’s Google Cloud Topples the Pi record article.

Back in 2017, we used y-cruncher to calculate 750 billion digits of Pi on Google Compute Engine. We used a 64-core instance, with 416GB of RAM, and tons of Local SSD + Persistent SSD.

At its peak CPU utilization, it consumed all 64 cores, and 5+TB of SSD storage.

In 2018, we were able to recalculate 750 billion digits in just 10 hours!

We also calculated 1 trillion digits of Pi using a 96-core 14TB n1-megamem-96 machine.

Serving the API

We developed the core backend using gRPC and Go. The gRPC service has two methods - one is a unary request/response, and another that takes advantage of server side streaming.

We then use grpc-gateway to expose the unary request/response as RESTful service.

The service definition looks like this:

syntax = "proto3";
package piaas;
import "google/api/annotations.proto";

message Range {
    int64 start = 1;
    int64 numberOfDigits = 2;
    int64 chunkSize = 3;
}
message ByteArray {
    string content = 1;
}
service PiaaS {
    rpc Get(Range) returns (ByteArray) {
      option (google.api.http) = {
        get: "/v1/pi"
      };
    }
    rpc Stream(Range) returns (stream ByteArray);
}

The RESTful API is also registered and managed by Google Cloud Endpoint, so that we can easily monitor and manage service deployments.

The services are deployed into a Google Container Engine (managed Kubernetes) cluster. The API endpoint, api.pi.delivery is exposed via a Kubernetes Ingress that’ll automatically create a Global HTTP(s) Load Balancer.

We used kube-logo to automatically provision a SSL certificate from Let’s Encrypt for HTTPS.

Serving This Site

Finally, this site is hosted on Firebase Hosting, which not only automatically serves the static content via a CDN, it also automatically provisions a SSL certificate for custom domain!

Who are We?

We are part of the Google Cloud Platform Developer Advocacy team.

Since 2015, different members of the team worked on different Pi Day demos!

In 2015, we calculated 250 billion digits of Pi, by Greg Wilson, Ray Tsang.

In 2016, we calculated 500 billion digits of Pi and made it searchable. The indexing peaked at 2-million writes per second. You can learn more how we did that in the blog Calculating and searching 500 billion digits of Pi, by Francesc Campoy, Jen Tong, Ray Tsang, Sara Robinson.

In 2017, we made this site and the Pi API, by Francesc Campoy, Guillaume Laforge, Ian Lewis, Ray Tsang, Sandeep Dinesh.

In 2018, the Pi API was updated to serve 1 trillion digits by Ray Tsang.

In 2019, the Pi digits world record was broken by Emma Haruka Iwao. Google Cloud Experiment for Pi Day was created by Mathias Paumgarten. The Pi API was updated to serve all 31.4+ trillion digits by Ray Tsang.

In 2020, we updated the Pi digits to 50 trillion digits, and added Machine Learning models to generate even better Pi music.