File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 )
1920end
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
You can’t perform that action at this time.
0 commit comments