HP Forums
Multinomial Coefficient - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Multinomial Coefficient (/thread-3854.html)



Multinomial Coefficient - salvomic - 05-15-2015 08:37 PM

hi all,
two version of a program to calculate Multinomial Coefficient.
This program accept an integer for "n" (total of k) and a list with brackets {} for the list of the k (like {1,4,4,2}) and give a integer that represent permutations in a multi set.

Enjoy!

Salvo Micciché

Code:

EXPORT Multinomial()
BEGIN
local n,k;
input ({n, {k}}, "Multinomial Coefficient", {"n", "k_i list"}, {"Total n", "input {k_i list} with comma"}, {0,0});
// we need integers (also for list)
n:=IP(n); k:=IP(k);

return n!/∏List(k!);
END;

A version in CAS, without input routine...

Code:

#cas
multinom(args):=
// Multinomial by Salvo M. multinom(n,{list k_i})
BEGIN
local n, k, argv,argc;
argv:=[args];
argc:=size(argv);
IF argc !=2 THEN
return "Input: integer n, {list k}"; 
ELSE
n:=argv(1);
k:=argv(2);
IF (type(k) != DOM_LIST) THEN return "Second argument must be a list"; ELSE
// controllo interi
 n:= ip(n); k:= ip(k);
 return  n!/∏LIST(k!);
END; // if inner

END; // if out

END;
#end

This program has a control if user doesn't input 2 arguments (no more, no less), if the second argument is not a list and rounds both arguments, to get integers.

Caveat: with this version (#cas program) integer part command -ip()- must be lowercase or ip(k) doesn't works for the list giving wrong results.

(Thanks Parisse for hint about the CAS version!)