The sum of consecutive numbers from $a$ to $b$ is
$$\frac{(a+b)(b-a+1)}{2}$$
hence simply
f[n_] := {a, b} /.
Solve[(a + b) (b - a + 1)/2 == n && 0 < a < n && 0 < b < n, {a, b}, Integers]
f[45] // AbsoluteTiming
{0.019466, {{1, 9}, {5, 10}, {7, 11}, {14, 16}, {22, 23}}}
It is straightforward and rather fast. As a test case:
f[4500] // AbsoluteTiming
{0.063403, {{23, 97}, {27, 98}, {78, 122}, {93, 132}, {168,
192}, {176, 199}, {293, 307}, {496, 504}, {559, 566}, {898,
902}, {1499, 1501}}}
This distribution is interesting:
ListPlot[tab = Table[{i, Length @ f[i]}, {i, 1000}], Filling -> Axis,
Frame -> True, PlotRange -> All,
FrameLabel -> {"n", "Length@f[n]"}]; // AbsoluteTiming
{12.3098, Null}

The biggest number of partitions (Length@f[n] == 15
) for n <= 1000
is for n = 945
:
Select[tab, #[[2]] == Max @ tab[[All,2]] &]
{{945, 15}}
while Length@f[944] == 1
and Length@f[946] == 3
.
It works also with huge numbers:
f[10^11] // AbsoluteTiming
{0.149738, {{60688, 451312}, {925363, 1027762}, {1240938,
1319062}, {4872573, 4893052}, {6392188, 6407812}, {24412015,
24416110}, {31998438, 32001562}, {159999688,
160000312}, {799999938, 800000062}, {3999999988,
4000000012}, {19999999998, 20000000002}}}
f[10^40] // AbsoluteTiming // Short
{1.55024,{{39445166261547851563,146819348661547851562},<<38>>,{<<1>>}}}
(I'm surprised it works so well, and I think that the timing is quite acceptable too.)
A sample rejection method (brute force, with just a little trick to use Ceiling[n/2]
):
n = 45;
MinMax /@
First /@ Select[
Flatten[#, 1] & @
Table[{Range[i, j], Total @ Range[i, j]}, {j, 1, Ceiling[n/2]}, {i, 1, j}],
#[[2]] == n &]; // AbsoluteTiming
{0.000803, Null}
works well for this simple case (however, on my laptop it needs 14 sec for n = 4000
) , but when
n = 4500;
it crashes the kernel.
Additionally,
g[n_] := Cases[FrobeniusSolve[Range @ n, n], {0 ..., 1 .., 0 ..}, Infinity]
is very slow:
(o = g[45]); // AbsoluteTiming
{43.871, Null}
although works:
MinMax /@ (Pick[Range@45, #, 1] & /@ o) // Sort
{{1, 9}, {5, 10}, {7, 11}, {14, 16}, {22, 23}}
Solve
works so well. Finally, it gained votes and obtained also two other answers, what I take as an indicator that this Q&A is not off topic. I'm not arguing, just presenting my motivation and opinion, to be clear. $\endgroup$