This is a question about the function nchoosek
in Matlab.
I want to find nchoosek(54,25)
, which is the same as 54C25. Since the answer is about 10^15, I originally use int64
. However the answer is wrong with respect to the symbolic one.
Input:
nchoosek(int64(54),int64(25))
nchoosek(sym(54),sym(25))
Output:
1683191473897753
1683191473897752
You can see that they differ by one. This is not really an urgent problem since I now use sym
. However can someone tell me why this happens?
EDIT:
I am using R2013a.
I take a look at the nchoosek.m
, and find that if the input are in int64
, the code can be simplified into
function c = nchoosek2(v,k)
n = v; % rename v to be n. the algorithm is more readable this way.
classOut = 'int64';
nd = double(n);
kd = double(k);
nums = (nd-kd+1):nd;
dens = 1:kd;
nums = nums./dens; %%
c = round(prod(nums));
c = cast(c,classOut);
end
However, the outcome of int64(prod(nums./dens))
is different from prod(sym(nums)./sym(dens))
for me. Is this the same for everyone?