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()
Recommended Posts