#【概要】
円周率に現れる数字の頻度を調べてみました。
ついでにネイピア数も調べてみました。
小数点以下を対象としています。
#【準備】
・PythonのライブラリPlotlyを使うため、APIキーをPlotlyから取得します。
(グラフを描画しない場合は必要なし)
・円周率とネイピア数から小数点以下の数字をテキストにコピペします。
(なお、ネイピア数に関しては最初の行が他の行と桁が違うため、0で埋める)
#【フォルダ構成】
|---scripts
|---pi.py(円周率の数字の頻度を調べる)
|---pi_graph.py(円周率の棒グラフ作成)
|---pi.txt(小数点以下の円周率)
|---e.py(ネイピア数の数字の頻度を調べる)
|---e_graph.py(ネイピア数の棒グラフ作成)
|---e.txt(小数点以下のネイピア数)
#【プログラムと結果】
グラフを表示するには
https://plot.ly/~ユーザ名/0/#plot
にアクセスすれば、表示できると思います。
# -*- coding: utf-8 -*-
zero = 0
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0
for line in open('pi.txt', 'r'):
for i in xrange(100):
if(line[i] == '0'):
zero += 1
elif(line[i] == '1'):
one += 1
elif(line[i] == '2'):
two += 1
elif(line[i] == '3'):
three += 1
elif(line[i] == '4'):
four += 1
elif(line[i] == '5'):
five += 1
elif(line[i] == '6'):
six += 1
elif(line[i] == '7'):
seven += 1
elif(line[i] == '8'):
eight += 1
elif(line[i] == '9'):
nine += 1
print zero #99959
print one #99758
print two #100026
print three #100229
print four #100230
print five #100359
print six #99548
print seven #99800
print eight #99985
print nine #100106
# -*- coding: utf-8 -*-
zero = -3 #テキストの最初の行を0で3つ埋めたため、減らしとく
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0
for line in open('e.txt', 'r'):
for i in xrange(60):
if(line[i] == '0'):
zero += 1
elif(line[i] == '1'):
one += 1
elif(line[i] == '2'):
two += 1
elif(line[i] == '3'):
three += 1
elif(line[i] == '4'):
four += 1
elif(line[i] == '5'):
five += 1
elif(line[i] == '6'):
six += 1
elif(line[i] == '7'):
seven += 1
elif(line[i] == '8'):
eight += 1
elif(line[i] == '9'):
nine += 1
print zero #498642
print one #500511
print two #499302
print three #501715
print four #500420
print five #500489
print six #499875
print seven #500015
print eight #499078
print nine #500290
# -*- coding: utf-8 -*-
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
plotly.tools.set_credentials_file(username='ユーザ名', api_key='APIキー')
data = [go.Bar(
x=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
y=[99959, 99758, 100026, 100229, 100230, 100359, 99548, 99800, 99985, 100106]
)]
py.iplot(data, filename='basic-bar')

# -*- coding: utf-8 -*-
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
plotly.tools.set_credentials_file(username='ユーザ名', api_key='APIキー')
data = [go.Bar(
x=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
y=[498642, 500511, 499302, 501715, 500420, 500489, 499875, 500015, 499078, 500290]
)]
py.iplot(data, filename='basic-bar')

#【考察】
円周率もネイピア数の数字も同じくらいの頻度で出現しているよう・・・
既に同じ頻度で出現するっていう数学の定理があるのかな?
(知っていたらコメントで教えていただきたいです)
#【参考サイト】
https://plot.ly/python/bar-charts/