Skip to content

Commit f4faddb

Browse files
committed
Added Madhava-Leibniz infinite sum
1 parent 23eb657 commit f4faddb

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

  • Pi_Algorithms/Lua/Yonaba

Pi_Algorithms/Lua/Yonaba/pi.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
-- Evaluates Pi using a random and statistical approach
55
-- (Monte-Carlo method) to evaluate pi
6+
-- See: http://en.wikipedia.org/wiki/Pi#Geometry_and_trigonometry
67
-- n : the number of simulations (should be high)
78
-- seed : (optional) a custom seed to init the RNG
89
-- returns: an approximation of pi
@@ -17,3 +18,16 @@ local function statistical_pi(n, seed)
1718
end
1819
return 4 * (count / n)
1920
end
21+
22+
-- Evaluates Pi using Madhava-Leibniz infinite series
23+
-- See: http://en.wikipedia.org/wiki/Pi#Infinite_series
24+
-- n : (optional) the order of the infinite sequence to reach (defaults to 100)
25+
-- returns: an approximation of pi
26+
local function madhava_leibniz_series(n)
27+
local k, sum = 0, 0
28+
n = n or 100
29+
for k = 0, n do
30+
sum = sum + ((-3)^(-k)) / (2 * k + 1)
31+
end
32+
return math.sqrt(12) * sum
33+
end

0 commit comments

Comments
 (0)