We can work backwards so to speak: f[r_, s_] := Union[Select[ FromDigits /@ Join @@ Permutations /@ IntegerPartitions[s, Ceiling[Log10[r]], Range[0, 9]], LessThan[r] ]] Example: f[1000, 6] > {6, 15, 24, 33, 42, 51, 60, 105, 114, 123, 132, 141, 150, 204, 213, 222, 231, 240, 303, 312, 321, 330, 402, 411, 420, 501, 510, 600} Total[IntegerDigits[f[1000, 6]], {2}] > {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6} Larger example: vals = f[10^12, 100]; // AbsoluteTiming > {0.069749, Null} Length[vals] > 75582 Verify: Total[IntegerDigits[vals], {2}] // Union > {100}