1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

日章旗で円周率を求めてみた

1
Last updated at Posted at 2019-03-30

Pythonを使って円周率を出すプログラムを作ってみました。
数学に興味を持ってもらうためのツールとしてPythonを活用しました。

pi.py
# 平成も終わりそうなので日章旗で円周率を求めてみた
%matplotlib inline
# 玉手箱3つ(matplotlib、random、math)を使う
import matplotlib.pyplot as plt
import random
import math

# dot(点)を0とする
dot = 0
# 1万個点を打つ
for i in range(10000):
  #点のx座標は1~100
  x = random.randint(1, 100)
  #点のy座標は1~100
  y = random.randint(1, 100)
  #中心から点までの距離(三平方の定理)
  d = math.sqrt((x-50)**2 + (y-50)**2)
  #もしdが50以下なら
  if (d <= 50):
  #dotに1を足す
    cnt += 1
  #赤色で点を打つ
    plt.scatter(x, y, marker='.', c='r')
  #その他の場合
  else:
  #白色で点を打つ
    plt.scatter(x,y,marker='.', c='w')
plt.axis('equal')
plt.show()

# 円周率を求める
p = dot / 10000
pi = p * 4
print(pi)
日章旗.png
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?