Coloring Digits of Pi

How can we visualize the digits of π using color?

One way to answer

First, how do we see the digits of π? Let’s look at 100 digits.

N[Pi,100]

To use these numbers to do things, it will help to have them in a list of digits. RealDigits will do this.

RealDigits[N[Pi,100]]

However, we have a list within a list. The 1 at the end indicates how many digits are to the left of the decimal.

So, let’s just take “part 1” of the answer. We do this with [[1]] at the end of the previous input. Give this a variable name so it is easier to use later, “digitlist” for example.

digitlist=RealDigits[N[Pi,100]][[1]]

Check that we have 100 digits by asking for the length of digitlist.

Length[digitlist]

What about color? Hue of a number between 0 and 1 will produce a color.

Hue[0.8]

Using Table we can create colors to correspond to each digit.

Table[Hue[n/10],{n,0,9}]

“Map” will allow us to put it all together. Map takes a function and applies it to each element of a list. See below for an example.

Map[f,{x,y,z}]

We can use Map to make every digit a color. (Note: This uses # and & for what is called a “pure function.” For now, just understand that each number in the list (digitlist) goes into the function where the # symbol is.)

Map[Hue[#/10]&,digitlist]

Displaying this answer

Rather than a list of color swatches, let’s put them together into something more artistic.

Partition the previous work into groups of 10.

Partition[Map[Hue[#/10]&,digitlist],10]

Display the partitioned list with Grid.

Grid[Partition[Map[Hue[#/10]&,digitlist],10]]

Add the option Spacings within the last ] for Grid and it will move everything closer together. See below.

Grid[Partition[Map[Hue[#/10]&,digitlist],10],Spacings->{0,0}]

What if we want more digits?

Start by putting it all together in one line of code by replacing “digitlist” with what that equals from above.

Grid[Partition[Map[Hue[#/10]&,RealDigits[N[Pi,100]][[1]]],10],Spacings->{0,0}]

Now we can adjust the numbers as desired.
Note: It will take more time if you make the numbers larger.
Don’t go crazy.
Try 400 digits of Pi.

Grid[Partition[Map[Hue[#/10]&,RealDigits[N[Pi,400]][[1]]],10],Spacings->{0,0}]

Try 400 digits with a partition of 20.

Grid[Partition[Map[Hue[#/10]&,RealDigits[N[Pi,400]][[1]]],20],Spacings->{0,0}]

What if we want different colors?

Change Hue to ColorData[“SunsetColors”] or ColorData[“Pastel”].

Grid[Partition[Map[ColorData["SunsetColors"][#/10]&,RealDigits[N[Pi,400]][[1]]],20],Spacings->{0,0}]

Grid[Partition[Map[ColorData["Pastel"][#/10]&,RealDigits[N[Pi,400]][[1]]],20],Spacings->{0,0}]

Or we can identify a specific digit.

Replace the color function with an If statement as below. (Note the double equal sign.)

Grid[Partition[Map[If[#==3,Hue[#/10],White]&,RealDigits[N[Pi,400]][[1]]],20],Spacings->{0,0}]

Additional Fun

Taking the individual digit example above, change “3” to “n” and put a Table around it for 0 ⩽ n ⩽ 9.
Note: Changed back to 100 digits and partition of 10.

Table[Grid[Partition[Map[If[#==n,Hue[#/10],White]&,RealDigits[N[Pi,100]][[1]]],10],Spacings->{0,0}],{n,0,9}]

Now, wrap ListAnimate around that.
(It may take a while before it plays smoothly.)

ListAnimate[Table[Grid[Partition[Map[If[#==n,Hue[#/10],White]&,RealDigits[N[Pi,100]][[1]]],10],Spacings->{0,0}],{n,0,9}]]

Any new questions?

Are some digits more common within the digits of π?

Histogram[RealDigits[N[Pi,100]][[1]]]

Are there other fun ways to represent the digits?

Sound[SoundNote[5]]

Sound[SoundNote[5,0.5]]

Sound[Map[SoundNote[#,0.2]&,RealDigits[N[Pi,100]][[1]]]]

What about other numbers?

N[E,100]

Grid[Partition[Map[Hue[#/10]&,RealDigits[N[E,100]][[1]]],10],Spacings->{0,0}]

N[GoldenRatio,100]

Sound[Map[SoundNote[#,0.2]&,RealDigits[N[GoldenRatio,100]][[1]]]]

Abby Brown
www.abbymath.com
CT@TP: Computational Thinking at Torrey Pines High School
December 2017