# Do you have some good program algorithm of this question?

the question is:
please input a positive integer that It is the sum of n(n>=2) consecutive integers.and please output all of the sequence of consecutive integers.A line for each sequence. Integers separated by spaces between.Each sequence from small to large.
e.g
input: 21
21=1+2+3+4+5+6
21=6+7+8

``````   so you should output:
1 2 3 4 5 6
6 7 8
...............
``````

Do you want the program to FIND all of these strings, or are you taking them as input?

IE: Is the input just the 21? Or the 21=1+2+3+4+5+6 too?

You can enter any of you want to enter a positive integer.Not just the 21

eg:
input:6
output: 1 2 3
1 5
2 4

1 5 and 2 4 are not valid outputs from the program you specified (CONSECUTIVE integer-sums)

thank you for correcting me

Okay, lets see if i can flex my math muscle. Someone will come up with a better way than me soon enough.
if F(N) = 1+…+N; N >= 2.

The maximum number of integers usable to sum to X is defined to be the maximum integer value M such that F(M) <= X.

``````
for(\$r = 2; \$r <= \$m; \$r++) {
for(\$s = 1; \$s <= (\$x / 2); \$s++) { //The bound on this needs refining, but my brain cant come up with the formula atm...
\$flag = true;
while(\$flag) {
\$cur = intsum(\$s,\$r);
if (\$cur == \$x) {
for(\$ins = 0;\$ins < \$r; \$ins++) {
echo (\$s+\$ins)." ";
}
echo "<br />";
}
if (\$cur >= \$x) {
\$flag = false;
}
}}}

function intsum(\$start,\$count) {
\$total = 0;
for(\$i = 0; \$i < \$count; \$i++) {
\$total += (\$start + \$i);
}
return \$total;
}

``````

well,It is very good.

I would if I change the conditions. let sequence to meet small to large.Not a continuous sequence also satisfy the needs of.
output all the result.
e.g
input 6
the result like this:
1,2,3
1,5
2,4
How to do what should?
thx.

change

``````for(\$r = 2; \$r <= \$m; \$r++) {
``````

into

``````for(\$r = \$m; \$r >= 2; \$r--) {
``````

And… somehow i managed not to declare what M should be…

``````
\$m = 2;
while(intsum(1,(\$m+1)) <= \$x) { \$m++; }

``````

This should work for your first requirement (sounds like homework??)

``````function seqSum(\$num)
{
\$stop = ceil(\$num/2);
\$seq = array();

for(\$i=1; \$i<=\$stop; \$i++) {
for(\$j=\$i, \$sum=0; \$j<=\$stop, \$sum<=\$num; \$j++) {
\$sum+=\$j;
if(\$sum === \$num) array_push(\$seq, range(\$i, \$j));
}
}
return \$seq;
}

\$num = 21;
\$res = seqSum(\$num);
foreach(\$res as \$r) {
echo implode('+', \$r).'='.\$num.'<br>';
}
``````

For your second, it doesn’t really make any sense. You want the list of all the numbers less than, say, 100 that add up to 100? 99+1, 98+2, 97+3, 97+1+2 … 50+1+2+3+4+5+6+7+8+9+… 50+1+49 … 50+10+40 …

I would if I change the conditions. let sequence to meet small to large.Not a continuous sequence also satisfy the needs of.
output all the result.
e.g
input 6
the result like this:
1,2,3
1,5
2,4
How to do what should?
thx.

If you want all possible sums to reach X of positive integers, you’re looking at an entirely different function… and will have a lot more output. I dont know offhand the math on the number of combinations… but for example, X will have floor(X/2) possible 2-number combinations for it… (There’s a formula somewhere that i’m trying to flesh out, but it’s eluding me at the moment)

I could be wrong here, but my math muscle spit this out for the size of the result set you’re talking about:

Number of Combinations (N) for a given Integer (X):
N(X) = Sigma(I,0…X-2) { Floor((X - I) / 2) }