473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sum of consecutive positive integers

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
6 8921
rgb
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
rgb
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
D_C
293 100+
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 = 1
  2.  2 = 2
  3.  3 = 1+2
  4.  4 = 4
  5.  5 = 2+3
  6.  6 = 1+2+3
  7.  7 = 3+4
  8.  8 = 8
  9.  9 = 2+3+4
  10. 10 = 1+2+3+4
  11. 11 = 5+6
  12. 12 = 3+4+5
  13. 13 = 6+7
  14. 14 = 2+3+4+5
  15. 15 = 1+2+3+4+5
  16. 16 = 16
  17. 17 = 8+9
  18. 18 = 3+4+5+6
  19. 19 = 9+10
  20. 20 = 2+3+4+5+6
  21. 21 = 1+2+3+4+5+6
  22. 22 = 4+5+6+7
  23. 23 = 11+12
  24. 24 = 7+8+9
  25. 25 = 3+4+5+6+7
  26. 26 = 5+6+7+8
  27. 27 = 2+3+4+5+6+7
  28. 28 = 1+2+3+4+5+6+7
  29. 29 = 14+15
  30. 30 = 4+5+6+7+8
  31. 31 = 15+16
  32. 32 = 32
  33. 33 = 3+4+5+6+7+8
  34. 34 = 7+8+9+10
  35. 35 = 2+3+4+5+6+7+8
  36. 36 = 1+2+3+4+5+6+7+8
Aug 31 '06 #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
Nov 23 '06 #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


Question is really good ,One Soln is given below . Run and check with diff input.

#include<stdio.h>
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<index;i++)
{
sum2+=i;
if((sum1-sum2) == num )
{
if((i+1) == index)
{
printf("NO ANSWER\n");
return 0 ;
}
printf("Num(%d) = ",num );
for(int temp= i+1;temp<=index;temp++)
printf(" %d +",temp);
printf("\b \n");
return 0 ;
}
}
printf("NO ANSWER\n");
return 0 ;
}
Nov 23 '06 #6
Question is really good ,One Soln is given below . Run and check with diff input.

#include<stdio.h>
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<index;i++)
{
sum2+=i;
if((sum1-sum2) == num )
{
if((i+1) == index)
{
printf("NO ANSWER\n");
return 0 ;
}
printf("Num(%d) = ",num );
for(int temp= i+1;temp<=index;temp++)
printf(" %d +",temp);
printf("\b \n");
return 0 ;
}
}
printf("NO ANSWER\n");
return 0 ;
}

Hi All ,

Above Code will not work for some specific input.

odd number has two solution.
suppose num is 9 then "9=2+3+4" OR "9=4+5"

Run the following code and test it .

#include<stdio.h>
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<index;i++)
{
sum2+=i;
if((sum1-sum2) == num )
{
if((i+1) == index)
{
// printf("NO ANSWER\n");
break;
}
Soln++;
printf("Solution-%d : %d = ",Soln,num );
for(int temp= i+1;temp<=index;temp++)
printf(" %d +",temp);
printf("\b \n");
break;
}
}

//if( Soln == 0 && num >= 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

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

Similar topics

7
by: mathon | last post by:
hi, i have the following recursive function: unsigned int sum_odds(unsigned int n) { if(n==1) return 1; else
19
by: Johs | last post by:
I need to make some special action if 'a' and 'b' are both positive or both negative. Is there some inbuilt function to check this? Johs
2
by: Michael21622 | last post by:
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=...
11
by: xctide | last post by:
This project will give you more experience on nested for loops and user-defined functions. The original project came from an IT company’s programming test where they were asking for the most...
1
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return...
6
by: cesco | last post by:
Hi, say I have a vector like the following: vec = and I'd like to know via a function (for example, ConsecutiveIntegers(vec, N)) whether there are N consecutive integers. So, for example,...
7
by: kepano | last post by:
pls help me to write a program (in c) such that we input an integer x,where x>0. For x, the program has to convert it into the sum of positive integers (all posible!). for e.g. x =8 1. 8 2....
63
by: deepak | last post by:
Hi, Can someone give the standard function which can create positive integer value in C? Thanks, Deepak
8
by: gigonomics | last post by:
Hi all, I hope someone can help me out. I need to return the best available seats subject to the constraint that the seats are side by side (or return X consecutive records from a table column...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.