Skip to main content
added 94 characters in body
Source Link
Greg Hurst
  • 36.8k
  • 1
  • 94
  • 143

We can work backwards so to speak:

f[r_, s_] := Sort[Select[Union[Select[
  FromDigits /@ Join @@ Permutations /@ IntegerPartitions[s, Ceiling[Log10[r]], Range[9]]Range[0, 9]], 
  LessEqualThan[r]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[1000Total[IntegerDigits[f[1000, 6]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 /@ valsTotal[IntegerDigits[vals], {2}] // Union
{100}

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}

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}
Source Link
Greg Hurst
  • 36.8k
  • 1
  • 94
  • 143

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}