開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の5章(集合と確率を操作する)、5.4(プログラミングチャレンジ)、問題5-5(円の領域を推定する、πの値を推定する)を取り組んでみる。
問題5-5(円の領域を推定する、πの値を推定する)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import math
def distance(x, y):
return math.sqrt(x**2 + y**2)
def estimate(radius, nums):
n = 0
m = 0
for _ in range(nums):
x0 = random.uniform(-radius, radius)
y0 = random.uniform(-radius, radius)
d = distance(x0, y0)
if d <= radius:
n += 1
else:
m += 1
return n / (n + m)
if __name__ == '__main__':
radius = float(input('Radius: '))
area = math.pi * radius**2
square_area = (2 * radius) ** 2
nums = [10**3, 10**5, 10**6]
for n in nums:
estimated = estimate(radius, n)
print('Area: {0}, Estimated ({1} darts): {2}'.format(
area, n, square_area * estimated))
for n in nums:
estimated = estimate(radius, n)
print('pi (math.pi): {0}, Estimated ({1} darts): {2}'.format(
math.pi, n, 4 * estimated))
入出力結果(Terminal, IPython)
$ ./sample5.py Radius: 2 Area: 12.566370614359172, Estimated (1000 darts): 12.576 Area: 12.566370614359172, Estimated (100000 darts): 12.5728 Area: 12.566370614359172, Estimated (1000000 darts): 12.577136 pi (math.pi): 3.141592653589793, Estimated (1000 darts): 3.216 pi (math.pi): 3.141592653589793, Estimated (100000 darts): 3.1436 pi (math.pi): 3.141592653589793, Estimated (1000000 darts): 3.14434 $ ./sample5.py Radius: 2 Area: 12.566370614359172, Estimated (1000 darts): 12.816 Area: 12.566370614359172, Estimated (100000 darts): 12.61504 Area: 12.566370614359172, Estimated (1000000 darts): 12.547936 pi (math.pi): 3.141592653589793, Estimated (1000 darts): 3.1 pi (math.pi): 3.141592653589793, Estimated (100000 darts): 3.13812 pi (math.pi): 3.141592653589793, Estimated (1000000 darts): 3.140856 $ ./sample5.py Radius: 10 Area: 314.1592653589793, Estimated (1000 darts): 319.6 Area: 314.1592653589793, Estimated (100000 darts): 313.96000000000004 Area: 314.1592653589793, Estimated (1000000 darts): 314.2164 pi (math.pi): 3.141592653589793, Estimated (1000 darts): 3.208 pi (math.pi): 3.141592653589793, Estimated (100000 darts): 3.13492 pi (math.pi): 3.141592653589793, Estimated (1000000 darts): 3.140992 $ ./sample5.py Radius: 10000 Area: 314159265.3589793, Estimated (1000 darts): 316800000.0 Area: 314159265.3589793, Estimated (100000 darts): 314296000.0 Area: 314159265.3589793, Estimated (1000000 darts): 314274800.0 pi (math.pi): 3.141592653589793, Estimated (1000 darts): 3.164 pi (math.pi): 3.141592653589793, Estimated (100000 darts): 3.13736 pi (math.pi): 3.141592653589793, Estimated (1000000 darts): 3.141904 $ ./sample5.py Radius: 0.001 Area: 3.141592653589793e-06, Estimated (1000 darts): 3.128e-06 Area: 3.141592653589793e-06, Estimated (100000 darts): 3.14228e-06 Area: 3.141592653589793e-06, Estimated (1000000 darts): 3.1410319999999997e-06 pi (math.pi): 3.141592653589793, Estimated (1000 darts): 3.152 pi (math.pi): 3.141592653589793, Estimated (100000 darts): 3.1382 pi (math.pi): 3.141592653589793, Estimated (1000000 darts): 3.14366 $
0 コメント:
コメントを投稿