A Pi API - Happy Pi Day logo

Happy Pi Day!

Congratulations to Emma Haruka Iwao for breaking the Pi digits world record by calculating 31,415,926,535,897 (31.4+ trillion) digits on Google Cloud Platform! Read Emma’s Calculating a record-breaking 31.4 trillion digits blog to learn more.

The Pi API is now serving all 31,415,926,535,897 digits! The Pi API can be used to build some fun demos, including listening and visualizing Pi digits.

Art

In anticipation of 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.

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 31.4 trillion digits and playing at 314BPM, it’ll play for ~100 billion minutes, or ~190,355 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 it 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.

/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, the Pi API and its demos were created in 2017 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.