/* Binomial in Picat. See http://rosettacode.org/wiki/Evaluate_binomial_coefficients Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go => Tests = [[10,3],[60,30],[100,50],[400,200]], foreach([N,K] in Tests) initialize_table, println([N,K,binomial_rec(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 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).