By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 429,087 Members | 1,513 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

sum of consecutive positive integers

 P: 5 pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5. If x=4 , the output should be "no answer" thanx in advance Aug 31 '06 #1
Share this Question
6 Replies

 P: 37 one solution might be: step 1: do a loop of 1+2+3+4+5+... until your SUM is greater than the INPUT and stop (or exit) the loop step 2: do a loop of SUM-1-2-3-4-... until your VALUE is equal to INPUT (which is the answer) OR less than the INPUT (which is no answer) note: you need to store 2 values, last added (i will call it y) and last subtracted+1 (i will call it x) for your final string answer doing x+(x+1)+(x+2)+... until you reach y. Aug 31 '06 #2

 P: 37 correction on step 1: the SUM should be equal or greater than (not equal only) and if it's equal (you got the answer), else (SUM is greater than INPUT) do step 2. Aug 31 '06 #3

 100+ P: 293 Interesting problem, and the solution is less than 30 lines of code. I would like to say that there will always be a trivial solution. N = sum(1,N) - sum(1,N-1). Surprisingly, the trend seems to be that this only happens when N is a power of 2. Expand|Select|Wrap|Line Numbers  1 = 1  2 = 2  3 = 1+2  4 = 4  5 = 2+3  6 = 1+2+3  7 = 3+4  8 = 8  9 = 2+3+4 10 = 1+2+3+4 11 = 5+6 12 = 3+4+5 13 = 6+7 14 = 2+3+4+5 15 = 1+2+3+4+5 16 = 16 17 = 8+9 18 = 3+4+5+6 19 = 9+10 20 = 2+3+4+5+6 21 = 1+2+3+4+5+6 22 = 4+5+6+7 23 = 11+12 24 = 7+8+9 25 = 3+4+5+6+7 26 = 5+6+7+8 27 = 2+3+4+5+6+7 28 = 1+2+3+4+5+6+7 29 = 14+15 30 = 4+5+6+7+8 31 = 15+16 32 = 32 33 = 3+4+5+6+7+8 34 = 7+8+9+10 35 = 2+3+4+5+6+7+8 36 = 1+2+3+4+5+6+7+8 Aug 31 '06 #4

 P: 7 pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5. If x=4 , the output should be "no answer" thanx in advance Nov 23 '06 #5

 P: 4 pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5. If x=4 , the output should be "no answer" thanx in advance Question is really good ,One Soln is given below . Run and check with diff input. #include int main() { int num,sum1=0,index=1,sum2=0; printf("Enter a no(>0) "); scanf("%d",&num); if(num <= 0 ) { printf("Wrong Input\n"); return 0; } for(index;;index++) { sum1=sum1+index; if(sum1>=num) break; } if(sum1 == num ) { if(num == index) { printf("NO ANSWER\n"); return 0 ; } printf("Num(%d) = ",num ); for(int temp=1;temp<=index;temp++) printf(" %d +",temp); printf("\b \n"); return 0; } for(int i=1;i

 P: 4 Question is really good ,One Soln is given below . Run and check with diff input. #include int main() { int num,sum1=0,index=1,sum2=0; printf("Enter a no(>0) "); scanf("%d",&num); if(num <= 0 ) { printf("Wrong Input\n"); return 0; } for(index;;index++) { sum1=sum1+index; if(sum1>=num) break; } if(sum1 == num ) { if(num == index) { printf("NO ANSWER\n"); return 0 ; } printf("Num(%d) = ",num ); for(int temp=1;temp<=index;temp++) printf(" %d +",temp); printf("\b \n"); return 0; } for(int i=1;i int main() { int num,sum1=0,index=1,sum2=0,Soln=0; printf("Enter a no(>0) "); scanf("%d",&num); if(num <= 0 ) { printf("Wrong Input\n"); return 0; } for(index;;index++) { sum1=sum1+index; if(sum1>=num) break; } if(sum1 == num ) { if(num == index) { printf("NO ANSWER\n"); return 0; } Soln++; printf("Solution-%d : %d = ",Soln,num ); for(int temp=1;temp<=index;temp++) printf(" %d +",temp); printf("\b \n"); } for(int i=1;i= 9 && num/2 == (num-1)/2 ) //--> if U don't want more than one ,soln use this if( num >= 9 && num/2 == (num-1)/2 ) //--> if U want more than one soln, use this { Soln++; printf("Solution-%d : %d = %d + %d\n",Soln,num ,num/2,num/2+1); return 0; } if(Soln==0) printf("NO SOLUTION\n"); return 0 ; } Nov 23 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.