A.I. Tools

Why You (Almost) Can’t Calculate Pi to a Billion Digits in Python at Home | by Bex T. | Oct, 2023

It is harder than you think

Bex T.
Towards Data Science
bexgboost_The_number_Pi_in_all_its_majesty_and_glory_mathematic_a06a71bf-6cb1-4585-86cf-315265ccf38e.png
Image by me with Midjourney.

Introduction

In June 9, 2022 Google set a new world record for calculating the most number of digits of Pi — 100 TRILLION! This monumental achievement was possible using the y-cruncher program running on Google Cloud. It crunched numbers for a whopping 157 days, 23 hours, 31 minutes and 7.651 seconds.

If one billion is 100 thousand times smaller than 100 trillion, would the runtime decrease accordingly? In other words, would it take only 136 seconds?

But 136 seconds is too ambitious. Home PCs are much less powerful than Google Cloud’s most formidable environments. So, how about a more reasonable runtime like 24 hours?

Turns out, calculating even a billion digits of Pi within 24 hours is a giant pipe dream. This article explains why with evidence in Python.

First of all, what is wrong with math.pi?

import math

print(math.pi)

3.141592653589793

math.pi has a precision of 15 digits. While it is not much, it is enough for the highest accuracy calculations in science.

For example, NASA’s Jet Propulsion Lab (JPL) uses 15 digits of Pi to navigate between planets. To give you an idea, this level of precision is enough to calculate the circumference of a circle with a radius of 15 billion miles. The resulting 94 billion miles circumference would be off by no more than the width of your little finger. Think about that!

So, why even bother with a billion digits, let alone a 100 trillion?

Well, to give you the geek’s answer: “Because it would be so freaking cool!”.

What if we increase the decimal precision?

Before we take out the big guns (the algorithms!), what if we increase decimal precision in Python when approximating π? That would be much simpler.

For the approximation, we will use Ramanujan’s formula for π. Mathematicians all over the world use it to approximate π to an insurmountable extent.

from decimal import Decimal…


Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Translate »