0

I have a sorted list of a large number of primes. I want to iterate over combinations of fixed size $n$ in increasing order of their sum. Naturally the standard approach for $n=4$:

$$s_0 = \sum(A, B, C, D)$$ $$s_1 = \sum(A, B, C, E)$$ $$s_2 = \sum(A, B, C, F)$$ $$...$$

will not suffice. Can a strategy be devised to accomplish this?

dmg
  • 191
  • how large is your list? Are you comfortable with using a programming language? I have some code in Mathematica that could apply to an arbitrary list. – Vincent Tjeng Feb 27 '13 at 14:21
  • Specifically, SortBy[Subsets[Table[Prime[i], {i, 6}], {4}], Total] will do so for the first $6$ primes and $n=4$, and in general SortBy[Subsets[Table[primelist, {n}], Total] will do so for primes stored in primelist and combinations of size $n$. – Vincent Tjeng Feb 27 '13 at 14:26
  • @VincentTjeng I'm comfortable with Python, C/C++ (I could probably understand some basic Mathematica). The list can contain millions of entries, so generating all subsets is not an option. That's why I'm trying to device an iterating strategy, but I'm not sure if it is possible. – dmg Feb 27 '13 at 14:36
  • so if I understand you would only like to generate the $k$ subsets with the smallest sum? – Vincent Tjeng Feb 27 '13 at 15:59
  • I want to iterate over all possible subsets until I find one that matches a given criteria and has the lowest sum among all that match the same criteria (for a fixed size). Theoretically my iterator should be able to go through all the subsets. Practically it will not be necessary. I've started thinking about making some sort of BFS where the front contains the candidates for lowest sum. Perhaps this could do it. – dmg Feb 27 '13 at 16:04
  • okay then, do post on this site if you find a solution! this is an intriguing problem. – Vincent Tjeng Feb 27 '13 at 16:09
  • just saw this - http://math.stackexchange.com/questions/89419/algorithm-wanted-enumerate-all-subsets-of-a-set-in-order-of-increasing-sums – Vincent Tjeng Feb 27 '13 at 16:13

0 Answers0