Created
December 29, 2019 14:16
-
-
Save YuukiToriyama/5164e3441a190cc0cda9833224b82218 to your computer and use it in GitHub Desktop.
モンテカルロ法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
function calcPi(m) | |
local count = 0 | |
for i = 1, m do | |
x, y = math.random(), math.random() | |
if x^2 + y^2 < 1 then | |
count = count + 1 | |
end | |
end | |
-- (円の面積):(正方形の面積) = r^2pi:4r^2 | |
-- (円の面積):(正方形の面積) = (円の中に入った点の数):(打った点の総数) | |
return 4 * count / m | |
end | |
print(calcPi(10000000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment