Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how do i find a consecutive series
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
addie1125




PostPosted: Thu Feb 05, 2009 5:29 pm   Post subject: how do i find a consecutive series

as the topic

how do i find a consecutive series of a number??
i need to write a program for this, but i don't even know how to do with my hand

for example:

the number, 39, is a input...and it has the consecutive series below:
4+5+6+7+8+9
12+13+14
19+20

the program also need to handle big number like 1020

Thank you very much
Sponsor
Sponsor
Sponsor
sponsor
jbking




PostPosted: Thu Feb 05, 2009 5:57 pm   Post subject: Re: how do i find a consecutive series

addie1125 @ Thu Feb 05, 2009 3:29 pm wrote:
for example:

the number, 39, is a input...and it has the consecutive series below:
4+5+6+7+8+9
12+13+14
19+20


I'm thinking there are Number Theory elements here along with some classical Algebra and modulo arithmetic.

Let's consider integer n and do some of the smaller cases, for integer k:

When k = 2:
If n is even, then there isn't a pair of consecutive integers that will sum to it.
If n is odd, then there is such a pair based on the floor and ceiling of n/2.

For k=3:
If n is divisible by 3,i.e. n mod 3 = 0, then there will exist a series, a-1,a,a+1 that will sum to n as 3*a = n.

For k=4:
We want to know if there is exists a solution of x+(x+1)+(x+2)+(x+3) = n or 4x+6 = n. So, n mod 4 has to equal 2 for there to be a solution here.

For k=5:
Now, consider the sequence of 5 consecutive integers, 5x+10 = n, thus n mod 5 = 0.

This does generalize a bit:
n mod k has to equal the sum of 1,2,.., k-1 which is ((k-1)(k-2)/2) which can tell you if it is possible.

To compute the values simply subtract that ((k-1)(k-2)/2) from n and then divide by k and add 0,1,2,..k-1 to get the integers.

Did I miss something here, other than for some low values of n this doesn't quite work out, e.g. n=1 has 0+1 as a possible solution or if n is less than or equal to 0.

I'm sorry for not writing out the Java to do this, but I think this is plenty of help for the problem.
addie1125




PostPosted: Mon Feb 09, 2009 1:52 am   Post subject: RE:how do i find a consecutive series

thank you, jbking
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: