21 lines
515 B
Text
21 lines
515 B
Text
local fmt = require "fmt"
|
|
|
|
local function mcpi(n)
|
|
local inside = 0
|
|
for _ = 1, n do
|
|
local x = math.random()
|
|
local y = math.random()
|
|
if x * x + y * y <= 1 then inside += 1 end
|
|
end
|
|
return 4 * inside / n
|
|
end
|
|
|
|
print("Iterations -> Approx Pi -> Error%")
|
|
print("---------- ---------- ------")
|
|
local n = 1000
|
|
while n <= 1e8 do
|
|
local pi = mcpi(n)
|
|
local err = math.abs(math.pi - pi) / math.pi * 100.0
|
|
fmt.print("%9d -> %10.8f -> %6.4f", n, pi, err)
|
|
n *= 10
|
|
end
|