Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,11 @@
|
|||
function Binomial( n, k )
|
||||
if k > n then return nil end
|
||||
if k > n/2 then k = n - k end -- (n k) = (n n-k)
|
||||
|
||||
numer, denom = 1, 1
|
||||
for i = 1, k do
|
||||
numer = numer * ( n - i + 1 )
|
||||
denom = denom * i
|
||||
end
|
||||
return numer / denom
|
||||
end
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
local Binomial = setmetatable({},{
|
||||
__call = function(self,n,k)
|
||||
local hash = (n<<32) | (k & 0xffffffff)
|
||||
local ans = self[hash]
|
||||
if not ans then
|
||||
if n<0 or k>n then
|
||||
return 0 -- not save
|
||||
elseif n<=1 or k==0 or k==n then
|
||||
ans = 1
|
||||
else
|
||||
if 2*k > n then
|
||||
ans = self(n, n - k)
|
||||
else
|
||||
local lhs = self(n-1,k)
|
||||
local rhs = self(n-1,k-1)
|
||||
local sum = lhs + rhs
|
||||
if sum<0 or not math.tointeger(sum)then
|
||||
-- switch to double
|
||||
ans = lhs/1.0 + rhs/1.0 -- approximate
|
||||
else
|
||||
ans = sum
|
||||
end
|
||||
end
|
||||
end
|
||||
rawset(self,hash,ans)
|
||||
end
|
||||
return ans
|
||||
end
|
||||
})
|
||||
print( Binomial(100,50)) -- 1.0089134454556e+029
|
||||
Loading…
Add table
Add a link
Reference in a new issue