Creation of art objects, games and much more using Pi and Python

Pi is perhaps the most famous number in the world. This is the only number for which a special day of the year was established – Pi Day, celebrated on March 14. In this article, we’ll take a look at the various features of π and try to create something fun with it using Python. For example, art objects, music, games and more. Open up the Colab notebook and start.

Colab and environment setup

Introduction to Colab

Google Colaboratory, or simply Colab, is a Python development environment using the Jupyter Notebook development tool to connect to Google’s powerful cloud computing resources and run Python code. Colab provides a runtime environment where you can install packages, access data online, navigate the file system, and save / restore data.

Google Colaboratory Notebook

Running code in Colab

Cells are of two types: code and text. The text boxes provide descriptions of the actions, and the code boxes can be triggered individually by pressing the play button. This Colab notebook contains 12 sections. To run a code in a specific code cell, click the play button in the upper left corner of this cell.

Note: You can use the Jupyter Notebook wrapper from the GitHub repository.

Install dependencies and download files

The first section installs all the dependencies and clones my GitHub repository so we can access the helper code and text files containing the digits of π. This cell must be started before all other cells. The code in this cell downloads various dependencies into the Linux environment and installs the python packages required to run the code.

1. What is pi?

Pi (π) is an irrational number. A number is called irrational if it cannot be represented as an arithmetic fraction, that is, its decimal representation is infinite, and the groups of digits in its notation are never cyclically repeated. In other words, we do not know the exact meaning of the number π and will never know.

In addition, Pi is a transcendental number, that is, a number that cannot be a nonzero root of a polynomial with rational coefficients. In fact, such a number is not algebraic. The most famous transcendental numbers are π and e.

Determining the value of π has become a centuries-long fun for enthusiastic mathematicians. IN this article tells about what progress mathematicians have achieved over time in calculating the decimal places of the number π. Today, the record for the number of decimal places calculated for π (50 trillion) is held by Timothy Mullican. He applied the algorithm for multithreaded calculation of the number π “y-cruncher” by Alexander Yeeand it took him 303 days.

Despite the fact that we do not know the exact meaning of the number π, its importance can hardly be overestimated – after all, it is invisibly present in all our lives. The number π is defined as the ratio of the circumference of a circle to its diameter; however, the areas in which this number is used go far beyond elementary geometry. If interested, you can see video on YouTube channel Numberphile (relatively old), in which interesting properties of the number π are examined in some detail.

2. How old are you π-folds?

Find out your π-fold age. Enter your birthday and your age in π-multiples of years and days.

3. Calculation of the approximate value of the number π

Calculate the approximate value of π by specifying the number of term-by-term summation elements. To calculate the approximate value of the number π, use Leibniz formula – an infinite alternating series, slowly converging to the limit π, see the formula below.

To run the code, specify the number of term-by-term summation elements (n) that should be used to calculate the approximate value of π.

4. Million digits of π

Would you like to know what the first million digits of π looks like in decimal and hexadecimal notation? Remember that calculating a million characters is a heavy load on the browser, and the computer may slow down. To clear the output window, after starting a cell, click the “x” icon to the left of the output window.

One million decimal digits of π

One million hexadecimal digits of π

5. Diagram of the distribution of digits of the number π

Let’s take the first million digits of π in decimal and hexadecimal notation systems, build a diagram of the distribution of the digits of π and try to find out if this number contains digits that occur in the first million digits more often than others.

The first diagram shows the distribution of the digits of the number π in decimal, the second – in hexadecimal notation. Diagrams are also written to the “output” folder as an interactive.html web page that can be downloaded from the file tab on the left side of the Colab notebook. Charts are generated using the Plotly bar chart function.

6. Chord diagram of the number π

A chord chart is a special kind of donut chart that displays arcs connecting one node to another. In our example, arcs connect consecutive digits of the number π. There are as many nodes as there are digits in total (0 to 9 for decimal and 0 to 15 for hexadecimal). Arcs represent transitions from one digit to another. For example, the first digits after the decimal point in the number π: “14159”. Start at 1 and draw an arc to 4, then draw an arc from 4 to 1, then an arc from 1 to 5, then an arc from 5 to 9, and so on.

The first figure is a chord diagram of the first million decimal digits of π. The second is a chord diagram of the first million hexadecimal digits of π. Diagrams are also written to the “output” folder as an interactive.html web page that can be downloaded from the file tab on the left side of the Colab notebook. Chord diagrams are generated using the Chord module.

7. Snake – pi version


Let’s play the pi version of the Snake? I have modified the game so that the snake can be moved in the direction of the different digits of the number π. My snake does not swallow apples, but decimal digits of the number π. Your goal is to eat as many digits of π as possible!

Note: this code will not work correctly in Google Colab, so I had to embed my project in it. You can view the source code here… The source code for this project was taken from @LorenzoCampos, for the pi version, I made the appropriate changes to it. Try to play!

8. Binary raster image of the number π


The number π can also be represented in binary form. I took the first million hexadecimal digits of π and converted them to binary byte-by-byte. To display the image on the screen, I presented the first million hexadecimal digits as a monochrome black and white image with a resolution of 2000×2000. Zeros are displayed as black pixels, ones as white pixels.

After running the code, the image in the output window will be white for a while. You will need to wait a bit as it usually takes a while to load an image in Colab. The image is also written to the “output” folder. It can be downloaded from the file tab on the left side of the Colab notebook. To generate an image in the script, the Pillow module is used.

9. Composing music using the number π

You can even compose music with π. The script below uses the numpy module to calculate waveforms and the scipy module to write the result to a.wav file. In order not to clutter up the notebook with a large amount of code, part of the code is implemented as a script call… This script maps a string note to specific frequencies and generates a selected waveform for each note based on its duration and BPM.

Experiment with the dictionary of sheet music, and you may be able to generate interesting melodies using the number π. Assign specific numeric values ​​to the keys on the keyboard. Comments should only be entered in the format specified below. In addition, you can set the waveform type by substituting the waveform argument in play_note (). This type can be sine, triangle, saw or square.

The 20th or 22nd line indicates whether decimal or hexadecimal digits of π will be used. By default, only the first 100 digits of π are used in the script. The more numbers, the longer the calculation takes.

A playback window will appear in the output window, in which you can listen to and evaluate the received music. The file can be saved in .wav format. The audio file is also written to the “output” folder. It can be downloaded via the file menu (on the left side of the notepad).

Explanations on the format of recording notes:

  • Note designations must begin with NOTE_.

  • The first letter denotes a note: A to G.

  • The first letter “S” stands for sharp (not flat).

  • The number represents the octave on the keyboard, 0 is the lowest, 8 is the highest.

10. Creating art objects using the number π in the Processing environment

Processing Is an environment that implements a simple and fast toolkit for programming images, animations and interfaces. The graphics library and integrated development environment (IDE) are provided to users free of charge. Objects are programmed in Java, but if you want, you can enable the mode Python mode

You can’t just run the Processing code in Google Colaboratory. First you need to download Processing and install the Python Mode add-on. Follow the instructions Getting Started Guides

This Processing script parses the first 10,000 decimal digits of π and displays them as colored circles. The color of the circle corresponds to the number.

The resulting drawing can be downloaded here… Code is provided to create the image. Here you can also view / download the script from my GitHub repository.

11. Using π to Find a Connection with Kevin Bacon

This script demonstrates how to use the digits of the number π as a pseudo-random number generator in the algorithm.

Let’s try to play a game Six steps to Kevin Bacon using the number π. Six Steps to Kevin Bacon is a game / thought experiment, a variation on the Six Handshake Theory. The theory of “six handshakes” is a sociological theory according to which any two people on Earth are separated by no more than five levels of common acquaintances (and, accordingly, six levels of connections). Since we are having fun here, we will choose an extremely frivolous way to study this theory – download random wikipedia page and we will follow the links on this page to other pages until we get to the Wikipedia page with an article about Kevin Bacon.

On the original page, the script will get a list of all Wikipedia articles that are linked to on that page. Then the script, sequentially applying the hexadecimal digits of the number π, determines which next page of Wikipedia will be navigated to.

The script below can take quite a long time to execute, but in the end, sooner or later, we will end up on an article about Kevin Bacon. My record for the minimum number of pages on the way to an article on Kevin Bacon is 2,254.

In addition to creating such entertaining things, Python provides the programmer with the broadest possibilities. This language has long become the main language for the field of data science, data analytics and web development, which is covered in the course. Fullstack Python developer… If you want to add yourself a new skill that will make you even cooler specialist – come!

find outhow to upgrade in other specialties or master them from scratch:

Other professions and courses

Similar Posts

Leave a Reply