We can work backwards so to speak: f[r_, s_] := Sort[Select[ FromDigits /@ Join @@ Permutations /@ IntegerPartitions[s, Ceiling[Log10[r]], Range[9]], LessEqualThan[r] ]] Example: f[1000, 6] > {6, 15, 24, 33, 42, 51, 114, 123, 132, 141, 213, 222, 231, 312, 321, 411} Total[IntegerDigits /@ f[1000, 6], {2}] > {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}