To the Top

The API of Pi (π) Digits

Buy Me A Coffee

This page implements a Javascript Ajax utility that calls the API to fetch digits of Math constant Pi.

The PI values are pre-computed and stored in various text files for fast retrieval via the API.

The first 10 digits of Pi (π) are 3.1415926535

100 Digits of PI

3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706

1000 Digits of PI

3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019

What is π (Pi)?

Pi (π) is the ratio of a circle's circumference to its diameter. This means that no matter how big or small a circle is, if you divide the distance around it (circumference) by the distance across it (diameter), you will always get approximately 3.14159.

Why is pi important?

Pi is a fundamental constant that appears in countless areas of mathematics and physics. Here's why it's so crucial:

Interesting Facts about Pi

API (Application Programming Interface)

The API following has a rate-limit 1 call per second.
https://uploadbeta.com/api/pi/?cached&n=100
It will return JSON-encoded data (First 100 digits of Pi):
"314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706"
Parameter n is the number of digits.

Get PI Digits - API Servers

All servers are subject to fair use policy:
  1. Load Balancer: https://api.justyy.workers.dev/api/

Complete API Source Code (PHP - Fetch)

  $n = 100;
  $maxn = 1000000;
  if (isset($_GET['n'])) {
    $n = (integer)$_GET['n'];
  }
  if ($n < 1) {
    $n = 1;
  }
  if ($n > $maxn) {
    die(json_encode("MAXIMUM $maxn DIGITS"));
  }
  $cache = array(
         100,
         1000,
         2500,
         5000,
         8000,
         10000,
         25000,
         50000,
         75000,
         100000,
         250000,
         375000,
         500000,
         750000,
         875000,
         1000000
  );
  foreach ($cache as $key) {
    $keyfile = "cache/$key";
    if (($key >= $n) && file_exists($keyfile)) {
      $data = substr(file_get_contents($keyfile), 0, $n);
      break;
    }
  }
  header("Access-Control-Allow-Origin: *");
  header('Content-Type: application/json');
  die(json_encode($data));

PI Compute Algorithm (Python Code)

More Benchmarking
def ComputePi(numdigits):
    pi = ""
    a = [2] * (10*numdigits / 3)
    nines = 0
    predigit = 0
    for j in xrange(0, numdigits):
        q = 0
        p = 2 * len(a) - 1
        for i in xrange(len(a)-1, -1, -1):
            x = 10*a[i] + q*(i+1)
            q, a[i] = divmod(x, p)
            p -= 2

        a[0] = q % 10
        q /= 10
        if q == 9:
            nines += 1
        elif q == 10:
            pi += chr(predigit + 1 + ord("0"))
	    pi += "0" * nines
            predigit = 0
            nines = 0
        else:
            pi += chr(predigit + ord("0"))
            predigit = q;
	    pi += "9" * nines
            nines = 0

    pi += chr(predigit + ord('0'))
    return pi

Share: List of Many Other Online Tools