I think you can solve this by building a tree. On each node you have a list of values. Assuming that the sum of this list is less than the required amount, let it call it S -, you can build no more than three children for this node: one, adding 1 to the list of this node, adding 2 and one by adding 3. The condition for building the child was that the sum of the new list will still be less than S. In the end, that is, when you cannot generate new nodes, you have all your sequences in the nodes of your tree.
EDIT: in C # my poor explanation would be this:
first:
public class Node { public Node() { Children = new List<Node>(); } public static int SumMax { get; set; } public List<int> Values { get; set; } public List<Node> Children { get; set; } public void AddChild(int data) { if (Values.Sum() + data < SumMax) { Node child = new Node(); child.Values = new List<int>(Values); child.Values.Add(data); Children.Add(child); for (int = data; i < 4; i++) { child.AddChild(i); } } } public void FillSequences(List<List<int>> sequences) { if (Values.Count != 0) { sequences.Add(Values); } foreach (Node child in Children) { child.FillSequences(sequences); } } }
then the main one:
void Main() { Node.SumMax = 10; Node root = new Node(); root.Values = new List<int>(); for (int i = 1; i < 4; i++) root.AddChild(i); List<List<int>> sequences = new List<List<int>>(); root.FillSequences(sequences);
I don’t know if this is standard enough, but it roughly corresponds to the task. Hope this fits your needs ...
EDIT : to avoid generating the same sequences, we can “order” a tree: a node cannot create a child with a lower value than it. Thus, in the AddChild method, we run the loop on the “data”, and not on 1.
source share