[PYTHON] CCC: coding crash course (4) Make the numbers appearing in pi 3.141562 .... into a histogram

Frequency distribution of numbers appearing in 3.141562 ....

Create a histogram of the numbers that appear after the decimal point of Pi.

Instead of focusing on efficiency, try it in a beginner's way.

First, get the number after the decimal point of PI at http://www.eveandersson.com/pi/digits/pi-digits?n_decimals_to_display=500&breakpoint=500.


char_pi = "14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912"

For example, to find out how many "0" s appear:


count_0 = 0
for i in range(500):
    s = char_pi[i]
    if (s == "0"):
        count_0 = count_0 +1

print( count_0 )

You can do the same to find out how many times each number appears. Finally, make a graph and visualize it.


list_digits = [0,1,2,3,4,5,6,7,8,9,]

list_count = [0,0,0,0,0,0,0,0,0,0,]

for d in range(10):
    for i in range(500):
        s = char_pi[i]
        if (s == str(d)):
            list_count[d] = list_count[d] +1
    print(d, list_count[d])


from matplotlib import pyplot

pyplot.plot(list_digits, list_count, 'o')

pyplot.plot(list_digits, list_count, '-')

pyplot.ylim([0, max(list_count)+5])
pyplot.xlabel('Number')
pyplot.ylabel('Count')
pyplot.xticks([0,1,2,3,4,5,6,7,8,9,])

pyplot.show()

Figure_pi_digits_500.png

Recommended Posts

CCC: coding crash course (4) Make the numbers appearing in pi 3.141562 .... into a histogram
CCC: coding crash course (1)
CCC: coding crash course (3)
CCC: coding crash course (2)
Arrange the numbers in a spiral shape
CCC: coding crash course (5) Find out the frequency of words and letters that appear in Steve Jobs speeches
Make a histogram for the time being (matplotlib)
Make a copy of the list in Python
Write a log-scale histogram on the x-axis in python
Make a note of what you want to do in the future with Raspberry Pi
Put the lists together in pandas to make a DataFrame
Is there a bias in the numbers that appear in the Fibonacci numbers?
Is there a secret to the frequency of pi numbers?