title: Efficient algorithm to find a combination, which summation is equal to a known number, in a set of number
body: <p>Let's say there is a set of number</p>
  
  <blockquote>
    <p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</p>
  </blockquote>
  
  <p>I want to find out several combinations in the set of number such that the summation of it equal to a known number, for example, 18. We can find out that 5, 6, 7 is matched (5+6+7=18). </p>
  
  <p>Numbers in a combination cannot be repeated and the number in a set may not be consecutive.</p>
  
  <p>I've wrote a C# program to do that. The program is random to pick up number to form a combination and check whether the summation of combination is equal to a known number. However, the combination the program found may be repeated and it makes the progress not effective.</p>
  
  <p>I am wondering whether there is any efficient algorithm to find out such combination.</p>
  
  <p>Here's part of my code.</p>
  
  <pre><code>        int Sum = 0;
          int c;
          List&lt;int&gt; Pick = new List&lt;int&gt;();
          List&lt;int&gt; Target = new List&lt;int&gt;() {some numbers}
  
          Target.Sort();
  
              while (!Target.Contains(Sum))
              {
                  if (Sum &gt; Target[Target.Count - 1])
                  {
                              Pick.Clear();
                              Sum = 0;
  
                  }
                  while (true)
                  {
                      if (Pick.IndexOf(c = Math0.rand(0, Set.Count - 1)) == -1)
                      {
                          Pick.Add(c);
                      }
  
                      //Summation Pick
                      Sum = 0;
                      for (int i = 0; i &lt; Pick.Count; i++)
                          Sum += Set[Pick[i]];
  
                      if (Sum &gt;= Target[Target.Count - 1])
                          break;
                  }
  
  
              }
  
              Result.Add(Pick);
  </code></pre>