1

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,c2,...,cn} and an integer n:

x1*c1+x2*c2+...+xn*cn=n

Please share your thoughts. Many thanks.

Qiang Li
  • 10,593
  • 21
  • 77
  • 148
  • 1
    Did you mean that the right hand side n is also the exact number of integers c1, c2, ..., cn? Or, can the right hand side be different, say, m? x1*c1+x2*c2+...+xn*cn == m – Andrew Moylan Feb 14 '11 at 01:31

2 Answers2

4

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?

Andrew Moylan
  • 2,893
  • 18
  • 20
1

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]
Simon
  • 14,631
  • 4
  • 41
  • 101