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.▼
<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>
<lang Picat>binomial_fac(N,K) = factorial(N) // factorial(K) // factorial(N-K).</lang>
<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===
▲<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]
|