Which is the best way to generate all $x_i$ where $\sum\limits_{i=1}^7 x_i = 1.0$?

73 Views Asked by At

I was wandering which is the best way to generate various combinations of $x_i$ such that $$\sum\limits_{i=1}^7 x_i = 1.0$$

where $ x_i \in \{0.0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0\}$

I can generate these using brute-force, i.e checking through all $ 11^7$ combinations and only taking those which satisfies our constraint, however I am interested to know if there is another approach for this. Any ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

You can use a recursive programming approach. Start from the top. If $1.0$ is in the set, you need $6$ that add to $0.0$-those are easy to find. Otherwise, see if $0.9$ is in it, then look for combinations of $6$ that add to $0.1$ I am imagining a function f(val, n, max) that splits val into n pieces with max the greatest allowable piece and returns a list of the solutions. It will be something like:

f(val, n, max):    
    if max leq val:    
    ret= f(val-max, n-1, max)
    prepend val to all the returns
  ret1=f(val, n, max-0.1)
return ret+ret1

where the first call gets the ones with val included and the second gets all the ones without. They will be sorted in decreasing order.

0
On

'Best way' is relative. Can be turned into an integer problem (multiply by 10). These then are the partitions of n=10 into at most 7 parts. See http://en.wikipedia.org/wiki/Partition_%28number_theory%29 especially 'restricted partitions'. Often well known in/to Computer Algebra Systems. The result can be condensed (using '1^2 2^4' for '1+1+2+2+2+2') to {1^4 2^3 ; 1^5 2 3 ; 1^6 4 ; 1^2 2^4 ; 1^3 2^2 3 ; 1^4 3^2 ; 1^4 2 4 ; 1^5 5 ; 2^5 ; 1 2^3 3 ; 1^2 2 3^2 ; 1^2 2^2 4 ; 1^3 3 4 ; 1^3 2 5 ; 1^4 6 ; 2^2 3^2 ; 2^3 4 ; 1 3^3 ; 1 2 3 4 ; 1 2^2 5 ; 1^2 4^2 ; 1^2 3 5 ; 1^2 2 6 ; 1^3 7 ; 3^2 4 ; 2 4^2 ; 2 3 5 ; 2^2 6 ; 1 4 5 ; 1 3 6 ; 1 2 7 ; 1^2 8 ; 5^2 ; 4 6 ; 3 7 ; 2 8 ; 1 9 ; 10}