Express a number as the sum of three or more consecutive numbers in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • miyakisan
    New Member
    • Apr 2020
    • 1

    Express a number as the sum of three or more consecutive numbers in c

    Program in c, to find the consecutive series of a number, example.
    Input:24
    Output: 7+8+9=24

    Another example
    Input:26
    Output:5+6+7+8
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    We aren't here to do all the work for you but if you post what you've tried so far, we can help you along.

    Comment

    • AjayGohil
      New Member
      • Apr 2019
      • 83

      #3
      Hiii,

      Refer Below equation:

      Sum of first n natural numbers = n * (n + 1)/2

      Sum of first (n + k) numbers = (n + k) * (n + k + 1)/2

      If N is sum of k consecutive numbers, then
      following must be true.

      N = [(n+k)(n+k+1) - n(n+1)] / 2

      OR

      2 * N = [(n+k)(n+k+1) - n(n+1)]


      you can try below code:

      Code:
      #include <stdio.h>
      void printNumber(int l,int f)
      {
          int x;
            for ( x = f; x<= l; x++) 
            {
            printf(" %d +",x);
            
            }
              f++;
      }
      
      void getNumber(int n)
      {
          int last,first;
          for ( last=1; last<n; last++) 
          { 
              for ( first=0; first<last; first++) 
              { 
                  if (2*n == (last-first)*(last+first+1)) 
                  { 
                      printf("%d =",n);
                      printNumber(last, first+1); 
                      return; 
                  } 
              } 
          } 
          
         printf("Not possible");  
      }
      
      int main()
      {
          int n;
          printf("Enter input:");
          scanf("%d",&n);
          getNumber(n);
          return 0;
      }

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        There's a slightly more efficient method that uses fewer iterations. Using the formula target = n*x + (n*(n-1)/2), given that x must be an integer, you can test that a solution exists for n consecutive numbers by using the mod operator.

        Comment

        • SioSio
          Contributor
          • Dec 2019
          • 272

          #5
          The answer is not necessarily one set.
          Code:
          void getNumber(int n)
          {
          	int A;
          	int B;
          	int C;
          	int D;
          	int max = n / 3 + 1;
          	int Y[1000];
          	int Z[1000];
          	for (A = 0; A <= max; A++) {
          		B = A * (A + 1) / 2;
          		Z[A] = B;
          		Y[A] = B - n;
          	}
          	for (C = 0; C <= max; C++) {
          		for (D = 0; D <= max; D++) {
          			if (Z[C] == Y[D]) {
          				printf("from %d to %d\n", C + 1, D);
          				break;
          			}
          		}
          	}
          }
          Or
          Code:
          void getNumber(int n)
          {
          	int A;
          	int B[1000];
          	int C[1000];
          	int D;
          	int max = n / 3 + 1;
          	for (A = 0; A <= max; A++)
          	{
          		B[A] = A * (A + 1) / 2;
          		C[A] = B[A] - n;
          		for (D = 0; D <= max; D++)
          		{
          			if (C[A] == B[D]) {
          				printf("from %d to %d\n", D+1,A);
          				break;
          			}
          		}
          	}
          }

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            This will find all solutions in the fewest iterations possible.
            Code:
            void findSolution(int target) {
                int i, solution;
                for(i=3; (i * (i+1))/2 <= target; i++) {
                    solution = target - (i * (i-1))/2;
                    if(solution % i == 0 && solution > 0){
                        solution = solution/i;
                        printf("\n%d consecutive numbers starting at %d", i, solution);
                    }
                }
            }

            Comment

            • jayclayton
              New Member
              • May 2020
              • 1

              #7
              Thanks, i understand it now :)

              Comment

              Working...