title: Find set of numbers in one collection that adds up to a number in another
body: <p>For a game I'm making I have a situation where I have a list of numbers &ndash; say [7, 4, 9, 1, 15, 2] (named <code>A</code> for this) &ndash; and another list of numbers &ndash; say [11, 18, 14, 8, 3] (named <code>B</code>) &ndash; provided to me.  The goal is to find all combinations of numbers in <code>A</code> that add up to a number in <code>B</code>.  For example:</p>
  
  <ul>
  <li>1 + 2 = 3</li>
  <li>1 + 7 = 8</li>
  <li>2 + 9 = 11</li>
  <li>4 + 7 = 11</li>
  <li>1 + 2 + 4 + 7 = 14</li>
  <li>1 + 2 + 15 = 18</li>
  <li>2 + 7 + 9 = 18</li>
  </ul>
  
  <p>...and so on.  (For purposes of this, <code>1 + 2</code> is the same as <code>2 + 1</code>.)</p>
  
  <p>For small lists like this, it's trivial to just brute-force the combinations, but I'm facing the possibility of seeing thousands to tens of thousands of these numbers and will be using this routine repeatedly over the lifespan of the application.  Is there any kind of elegant algorithm available to accomplish this in reasonable time with 100% coverage?  Failing this, is there any kind of decent heuristics I can find that can give me a "good enough" set of combinations in a reasonable amount of time?</p>
  
  <p>I'm looking for an algorithm in pseudo-code or in any decently popular and readable language (note the "and" there....;) or even just an English description of how one would go about implementing this kind of search.</p>
  
  <hr>
  
  <p><em>Edited to add:</em></p>
  
  <p>Lots of good information provided so far.  Thanks guy!  Summarizing for now:</p>
  
  <ul>
  <li>The problem is NP-Complete so there is no way short of brute force to get 100% accuracy in reasonable time.</li>
  <li>The problem can be viewed as a variant of either the <a href="http://en.wikipedia.org/wiki/Subset_sum_problem" rel="noreferrer">subset sum</a> or <a href="http://en.wikipedia.org/wiki/Knapsack_problem#Greedy_approximation_algorithm" rel="noreferrer">knapsack</a> problems.  There are well-known heuristics for both which may be adaptable to this problem.</li>
  </ul>
  
  <p>Keep the ideas coming!  And thanks again!</p>