Jump to content

Evaluate binomial coefficients: Difference between revisions

(→‎{{header|APL}}: Add implementation.)
Line 2,075:
}
</lang>
 
=={{header|Picat}}==
<lang Picat>go =>
Tests = [[10,3],[60,30],[100,50],[400,200]],
foreach([N,K] in Tests)
println([N,K,binomial_it(N,K)])
end,
nl.
 
 
% Iterative
binomial_it(N,K) = Res =>
if K < 0 ; K > N then
R = 0
else
R = 1,
foreach(I in 0..K-1)
R := R * (N-I) // (I+1)
end
end,
Res = R.
 
% Using the built-in factorial/1
binomial_fac(N,K) = factorial(N) // factorial(K) // factorial(N-K).
 
% Recursive function (tabled)
table
binomial_rec(_N, 0) = 1.
binomial_rec(0, _K) = 0.
binomial_rec(N, K) = binomial_rec(N-1,K-1) + binomial_rec(N-1,K).</lang>
 
All returns:
<pre>
[10,3,120]
[60,30,118264581564861424]
[100,50,100891344545564193334812497256]
[400,200,102952500135414432972975880320401986757210925381077648234849059575923332372651958598336595518976492951564048597506774120]</pre>
 
binomial_rec/2 is a little slower than the two other (0.036s vs 0.002s on these tests).
 
=={{header|PicoLisp}}==
495

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.