From the comments in the question and other answers, it seems to me that the OP already knows how to calculate all the combinations whose sum is a target number (that's probably what the Solver
in the question does). What I think he wants is to get the combination with the least amount of numbers.
I have a couple of solutions, since I'm not really sure what you want:
1) If you want all the combinations with the least of amount of numbers, do this:
public static void Main()
{
// Here I have hard-coded all the combinations,
// but in real life you would calculate them.
// Probably using your `Solver` or any of the other answers in this page.
var combinations = new List<List<decimal>>{
new List<decimal>{ 1, 2, 3, 4, 5 },
new List<decimal>{ 1, 2, 5, 7 },
new List<decimal>{ 1, 3, 4, 7 },
new List<decimal>{ 1, 3, 5, 6 },
new List<decimal>{ 2, 3, 4, 6 },
new List<decimal>{ 2, 6, 7 },
new List<decimal>{ 3, 5, 7 },
new List<decimal>{ 4, 5, 6 }
};
// Filter the list above to keep only the lists
// that have the least amount of numbers.
var filteredCombinations = LeastNumbers(combinations);
foreach (var combination in filteredCombinations)
{
Console.WriteLine(string.Join("\t", combination));
}
}
public static List<List<decimal>> LeastNumbers(List<List<decimal>> combinations)
{
// First get the count for each combination, then get the minimum of those.
int smallestLength = combinations.Select(l => l.Count).Min();
// Second, only keep those combinations which have a count equals to the value calculated above.
return combinations.Where(l => l.Count == smallestLength).ToList();
}
Output:
2 6 7
3 5 7
4 5 6
2) If you only want one of the combinations with the least amount of numbers, do this instead:
public static void Main()
{
// Here I have hard-coded all the combinations,
// but in real life you would calculate them.
// Probably using your `Solver` or any of the answers in this page.
var combinations = new List<List<decimal>>{
new List<decimal>{ 1, 2, 3, 4, 5 },
new List<decimal>{ 1, 2, 5, 7 },
new List<decimal>{ 1, 3, 4, 7 },
new List<decimal>{ 1, 3, 5, 6 },
new List<decimal>{ 2, 3, 4, 6 },
new List<decimal>{ 2, 6, 7 },
new List<decimal>{ 3, 5, 7 },
new List<decimal>{ 4, 5, 6 }
};
// Filter the list above to keep only the first list
// that has the least amount of numbers.
var filteredCombination = LeastNumbers(combinations);
Console.WriteLine(string.Join("\t", filteredCombination));
}
public static List<decimal> LeastNumbers(List<List<decimal>> combinations)
{
// First get the count for each combination,
// then get the minimum of those.
int smallestLength = combinations.Select(l => l.Count).Min();
// Second, get only one of the combinations that have a count
// equals to the value calculated above.
return combinations.First(l => l.Count == smallestLength);
}
Output:
2 6 7
3) The OP also mentioned a max value of 3. So, if you know that number before-hand, you can do this:
public static void Main()
{
// Here I have hard-coded all the combinations,
// but in real life you would calculate them.
// Probably using your `Solver` or any of the answers in this page.
var combinations = new List<List<decimal>>{
new List<decimal>{ 1, 2, 3, 4, 5 },
new List<decimal>{ 1, 2, 5, 7 },
new List<decimal>{ 1, 3, 4, 7 },
new List<decimal>{ 1, 3, 5, 6 },
new List<decimal>{ 2, 3, 4, 6 },
new List<decimal>{ 2, 6, 7 },
new List<decimal>{ 3, 5, 7 },
new List<decimal>{ 4, 5, 6 }
};
// This must be known before hand.
// That's why I think my first solution is more usefull.
int max = 3;
// Filter the list above to keep only the lists
// that have a count less or equal to a predetermined maximum.
var filteredCombinations = FilterByMaxLength(combinations, max);
foreach (var combination in filteredCombinations)
{
Console.WriteLine(string.Join("\t", combination));
}
}
public static List<List<decimal>> FilterByMaxLength(List<List<decimal>> combinations, int max)
{
return combinations.Where(l => l.Count <= max).ToList();
}
2 6 7
3 5 7
4 5 6
Note: In a real scenario, you would also want to do some checking in those functions, like checking for null or empty lists.