📜  将数字表示为连续数字的总和

📅  最后修改于: 2021-04-26 08:19:59             🧑  作者: Mango

给定一个数字N,编写一个函数将N表示为两个或多个连续正数之和。如果没有解决方案,则输出-1。如果有多种解决方案,请打印其中一种。

例子:

Input : N = 10
Output : 4 + 3 + 2 + 1

Input  : N = 8
Output : -1

Input  : N = 24
Output : 9 + 8 + 7
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)]

下面是基于以上思想的实现。

C++
// C++ program to print a consecutive sequence
// to express N if possible.
#include 
using namespace std;
  
// Print consecutive numbers from
// last to first
void printConsecutive(int last, int first)
{
    cout << first++;
    for (int x = first; x<= last; x++)
        cout << " + " << x;
}
  
void findConsecutive(int N)
{
    for (int last=1; last


Java
// Java program to print a consecutive sequence
// to express N if possible.
class GfG
{
    // Print consecutive numbers from
    // last to first
    static void printConsecutive(int last, int first)
    {
        System.out.print(first++);
        for (int x = first; x<= last; x++)
            System.out.print(" + "+x);
    }  
      
    static void findConsecutive(int N)
    {
        for (int last=1; last


Python3
# Python3 program to print a consecutive
# sequence to express N if possible.
  
# Print consecutive numbers 
# from last to first
def printConsecutive(last, first):
    print (first, end = "")
    first += 1
    for x in range(first, last + 1):
        print (" +", x, end = "")
  
def findConsecutive(N):
    for last in range(1, N):
          
        for first in range(0, last):
              
            if 2 * N == (last - first) * (last + first + 1):
                print (N, "= ", end = "")
                printConsecutive(last, first + 1)
                return
    print ("-1")
  
# Driver code
n = 12
findConsecutive(n)
  
# This code is contributed by Shreyanshi Arun.


C#
// C# program to print a consecutive sequence
// to express N if possible.
using System;
  
class GfG
{
    // Print consecutive numbers from
    // last to first
    static void printConsecutive(int last, int first)
    {
        Console.Write(first++);
        for (int x = first; x <= last; x++)
            Console.Write(" + "+x);
    } 
      
    static void findConsecutive(int N)
    {
        for (int last = 1; last < N; last++)
        {
            for (int first = 0; first < last; first++)
            {
                if (2 * N == (last - first) 
                    * (last + first + 1))
                {
                    Console.Write(N + " = ");
                    printConsecutive(last, first + 1);
                    return;
                }
            }
        }
        Console.Write("-1");
    }
      
    // Driver code
    public static void Main () 
    {
        int n = 12;
        findConsecutive(n);
    }
}
  
// This code is contributed by vt_m


PHP


输出:

12 = 3 + 4 + 5

参考 :
https://math.stackexchange.com/questions/139842/in-how-many-ways-can-a-number-be-expressed-as-a-sum-of-conecutive-numbers