Skip to content

Commit 7c6d251

Browse files
committed
Added Viète infinite root series
1 parent 939e955 commit 7c6d251

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

  • Pi_Algorithms/Lua/Yonaba

Pi_Algorithms/Lua/Yonaba/pi.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,32 @@ local function ramanujan_series(n)
5252
return 1/(((2*math.sqrt(2))/9801)*sum)
5353
end
5454

55+
-- Evaluates Pi using Chudnovsky rapidly converging infinite series
56+
-- See: http://en.wikipedia.org/wiki/Pi#Infinite_series
57+
-- n : (optional) the order of the infinite sequence to reach (defaults to 25)
58+
-- returns: an approximation of pi
59+
local function chudnovsky_series(n)
60+
local sum = 0
61+
n = n or 25
62+
for k = 0, n do
63+
local num = ((-1)^k)*factorial(6*k)*(13591409+545140134*k)
64+
local denum = factorial(3*k)*(factorial(k)^3)*(640320^(3*k+3/2))
65+
sum = sum+(num/denum)
66+
end
67+
return 1/(12*sum)
68+
end
69+
70+
-- Evaluates Pi using Viète infinite root sum
71+
-- See: http://en.wikipedia.org/wiki/Pi#Infinite_series
72+
-- n : (optional) the order of the infinite sequence to reach (defaults to 100)
73+
-- returns: an approximation of pi
74+
local function viete_series(n)
75+
local a = math.sqrt(2)
76+
local prod = a/2
77+
n = n or 100
78+
for k = 2, n do
79+
a = math.sqrt(a+2)
80+
prod = prod*(a/2)
81+
end
82+
return 2/prod
83+
end

0 commit comments

Comments
 (0)