sum of consecutive positive integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaveta
    New Member
    • Aug 2006
    • 5

    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
  • rgb
    New Member
    • Aug 2006
    • 37

    #2
    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.

    Comment

    • rgb
      New Member
      • Aug 2006
      • 37

      #3
      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.

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        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.
        Code:
         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

        Comment

        • pixcee2000
          New Member
          • Nov 2006
          • 7

          #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

          Comment

          • sumanjha
            New Member
            • Nov 2006
            • 4

            #6
            Originally posted by pixcee2000
            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,inde x=1,sum2=0;
            printf("Enter a no(>0) ");
            scanf("%d",&num );
            if(num <= 0 )
            {
            printf("Wrong Input\n");
            return 0;
            }
            for(index;;inde x++)
            {
            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<=in dex;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 ;
            }

            Comment

            • sumanjha
              New Member
              • Nov 2006
              • 4

              #7
              Originally posted by sumanjha
              Question is really good ,One Soln is given below . Run and check with diff input.

              #include<stdio. h>
              int main()
              {
              int num,sum1=0,inde x=1,sum2=0;
              printf("Enter a no(>0) ");
              scanf("%d",&num );
              if(num <= 0 )
              {
              printf("Wrong Input\n");
              return 0;
              }
              for(index;;inde x++)
              {
              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<=in dex;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,inde x=1,sum2=0,Soln =0;
              printf("Enter a no(>0) ");
              scanf("%d",&num );
              if(num <= 0 )
              {
              printf("Wrong Input\n");
              return 0;
              }
              for(index;;inde x++)
              {
              sum1=sum1+index ;
              if(sum1>=num)
              break;
              }
              if(sum1 == num )
              {
              if(num == index)
              {
              printf("NO ANSWER\n");
              return 0;
              }
              Soln++;
              printf("Solutio n-%d : %d = ",Soln,num );
              for(int temp=1;temp<=in dex;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("Solutio n-%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("Solutio n-%d : %d = %d + %d\n",Soln,num ,num/2,num/2+1);
              return 0;
              }
              if(Soln==0)
              printf("NO SOLUTION\n");
              return 0 ;
              }

              Comment

              Working...