I have a problem similar to IntegerPartitions
function, in that I want to list all non-negative integer xi
's such that, for a given list of integers {c1,c开发者_如何转开发2,...,cn}
and an integer n
:
x1*c1+x2*c2+...+xn*cn=n
Please share your thoughts. Many thanks.
The built-in function FrobeniusSolve
solves the case where the c1, c2, ..., cn are positive integers (and the right hand side is not n):
In[1]:= FrobeniusSolve[{2, 3, 5, 6}, 13]
Out[1]= {{0, 1, 2, 0}, {1, 0, 1, 1}, {1, 2, 1, 0}, {2, 1, 0, 1}, {2,
3, 0, 0}, {4, 0, 1, 0}, {5, 1, 0, 0}}
Is this the case you need, or do you need negative c1, c2, ..., cn also?
Construct your list of ci
's and coefficients using
n = 10;
cList = RandomInteger[{1, 20}, n]
xList = Table[Symbol["x" <> ToString[i]], {i, n}]
Then, if there's a set of solutions for non-negative xi
's, it will be found by
Reduce[cList.xList == n && And@@Thread[xList >= 0], xList, Integers]
精彩评论