LoginSignup
3
4

More than 5 years have passed since last update.

円周率の数字の頻度に秘密はあるのか?

Last updated at Posted at 2016-08-09

#【概要】
  円周率に現れる数字の頻度を調べてみました。
  ついでにネイピア数も調べてみました。
  小数点以下を対象としています。

#【準備】
 ・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
にアクセスすれば、表示できると思います。

pi.py
# -*- 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
e.py
# -*- 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
pi_graph.py
# -*- 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')
pi.png
e_graph.py
# -*- 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')
e.png

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

#【参考サイト】
 https://plot.ly/python/bar-charts/

3
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4