Evaluate binomial coefficients: Difference between revisions

→‎{{header|Picat}}: Moved into subsections. Added {{out}}
(→‎{{header|Picat}}: Moved into subsections. Added {{out}})
Line 2,077:
 
=={{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===
 
<lang Picat>binomial_it(N,K) = Res =>
% Iterative
binomial_it(N,K) = Res =>
if K < 0 ; K > N then
R = 0
Line 2,095 ⟶ 2,088:
end
end,
Res = R.</lang>
 
% ===Using the built-in factorial/1===
<lang Picat>binomial_fac(N,K) = factorial(N) // factorial(K) // factorial(N-K).</lang>
 
% Recursive function===Recursion (tabled)===
<lang Picat>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>
 
===Test===
All returns:
<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.
</lang>
 
 
All methods prints the same result.
 
{{out}}
<pre>
[10,3,120]
495

edits