Pi Day 2017 logo

Happy Pi Day!

To celebrate 2017 Pi Day, we created a Pi API that has access to 750 billion digits of Pi. We used it to build some fun demos, including listening and visualizing Pi digits.

This is just for fun. There is NO SLA. It might be turned off at any time!

Visualize

This demo visualizes digit transitions 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.

Listen

This demo plays midi piano notes for each digit of Pi. Each digit plays a different note so you can get to know Pi via music!

Loading sound samples. Please wait...

Google Home

We also created a demo Google Home assistant! If you have a Google Home, you can:

  1. Say: “Ok Google, talk to Pi Digit agent.”
  2. Ask: “What is the 10th digit of Pi?”

Calculating Pi

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.

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.

Writing the Assistant

We used API.AI and Google Cloud Functions to develop the demo Google Home assistant. Learn more technical details on Google Home helps you learn the digits of Pi.

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.

This demo was created by Francesc Campoy, Guillaume Laforge, Ian Lewis, Ray Tsang, Sandeep Dinesh

In the past, different members of the team worked on different Pi Day demos!

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 2015, we calculated 250 billion digits of Pi, by Greg Wilson, Ray Tsang

/v1/pi

Get Pi Digits

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

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

$.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>
{
  "start": 0,
  "numberOfDigits": 100
}
{
  "content": "314159...",
}

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();