Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@takahashikaito94

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

Pythonを使って円周率を出すプログラムを作ってみました。

pi.py
``````#平成も終わりそうなので日章旗で円周率を求めてみた
%matplotlib inline
#玉手箱３つ(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に１を足す
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)
``````

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?