📜  前N个自然数的和系列的和

📅  最后修改于: 2021-04-22 01:35:43             🧑  作者: Mango

给定自然数n ,找到前N个自然数的和系列之和。

例子:

简单方法:
查找从1到N的每个值的和系列,然后将其相加。

  • 创建一个变量Total_sum来存储所需的求和序列。
  • 从1到N遍历数字
  • 通过使用公式sum =(N *(N + 1))/ 2来找到每个值的和系列
  • 将值添加到Total_sum

最后,打印存储在Total_sum中的值。

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
// Function to find the sum
static long sumOfSumSeries(int N)
{
    long sum = 0L;
 
    // Calculate sum-series
    // for every natural number
    // and add them
    for (int i = 1; i <= N; i++)
    {
        sum = sum + (i * (i + 1)) / 2;
    }
 
    return sum;
}
 
// Driver code
int main()
{
    int N = 5;
    cout << sumOfSumSeries(N);
}
 
// This code is contributed by Code_Mech


Java
// Java program to implement
// the above approach
 
class GFG {
 
    // Function to find the sum
    static long sumOfSumSeries(int N)
    {
 
        long sum = 0L;
 
        // Calculate sum-series
        // for every natural number
        // and add them
        for (int i = 1; i <= N; i++) {
            sum = sum + (i * (i + 1)) / 2;
        }
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 5;
        System.out.println(sumOfSumSeries(N));
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the sum
def sumOfSumSeries(N):
 
    _sum = 0
 
    # Calculate sum-series
    # for every natural number
    # and add them
    for i in range(N + 1):
        _sum = _sum + (i * (i + 1)) // 2
 
    return _sum
 
# Driver code
N = 5
 
print(sumOfSumSeries(N))
     
# This code is contributed by divyamohan123


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the sum
static long sumOfSumSeries(int N)
{
    long sum = 0L;
 
    // Calculate sum-series
    // for every natural number
    // and add them
    for(int i = 1; i <= N; i++)
    {
       sum = sum + (i * (i + 1)) / 2;
    }
     
    return sum;
}
 
// Driver code
public static void Main()
{
    int N = 5;
     
    Console.Write(sumOfSumSeries(N));
}
}
 
// This code is contributed by Nidhi_Biet


Javascript


C++
// C++ program to implement
// the above approach
#include 
#include 
using namespace std;
 
// Function to find the sum
long sumOfSumSeries(int n)
{
    return (n * (n + 1) * (n + 2)) / 6;
}
 
// Driver code
int main ()
{
    int N = 5;
    cout << sumOfSumSeries(N);
    return 0;
}
 
// This code is contributed
// by shivanisinghss2110


Java
// Java program to implement
// the above approach
 
class GFG {
 
    // Function to find the sum
    static long sumOfSumSeries(int n)
    {
        return (n * (n + 1) * (n + 2)) / 6;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 5;
        System.out.println(sumOfSumSeries(N));
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the sum
def sumOfSumSeries(n):
     
    return (n * (n + 1) * (n + 2)) // 6
 
# Driver code
N = 5
 
print(sumOfSumSeries(N))
 
# This code is contributed by divyamohan123


C#
// C# program to implement the
// above approach
using System;
class GFG{
 
// Function to find the sum
static long sumOfSumSeries(int n)
{
    return (n * (n + 1) * (n + 2)) / 6;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 5;
     
    Console.Write(sumOfSumSeries(N));
}
}
 
// This code is contributed by Ritik Bansal


Javascript


输出:
35

时间复杂度:O(N)
高效的方法:
可以使用以下公式直接计算以上系列的total_sum:

以上公式的证明:
假设N = 5

  1. 然后总和就是表中所有下面元素的总和,我们称其为“结果
Natural number 1 2 3 4 5 6
Sum of natural number (sum-series) 1 3 6 10 15 21
Sum of sum-series 1 4 10 20 35 56

让我们在其他列中填充具有相同值的空单元格,将其称为“ totalSum

1        
1 2      
1 2 3    
1 2 3 4  
1 2 3 4 5
  1. 自从,
  1. 所以
    \text{Sum of Sum-Series till N} = \frac{(N*(N+1)*(N+2))}{6}

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
#include 
#include 
using namespace std;
 
// Function to find the sum
long sumOfSumSeries(int n)
{
    return (n * (n + 1) * (n + 2)) / 6;
}
 
// Driver code
int main ()
{
    int N = 5;
    cout << sumOfSumSeries(N);
    return 0;
}
 
// This code is contributed
// by shivanisinghss2110

Java

// Java program to implement
// the above approach
 
class GFG {
 
    // Function to find the sum
    static long sumOfSumSeries(int n)
    {
        return (n * (n + 1) * (n + 2)) / 6;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 5;
        System.out.println(sumOfSumSeries(N));
    }
}

Python3

# Python3 program to implement
# the above approach
 
# Function to find the sum
def sumOfSumSeries(n):
     
    return (n * (n + 1) * (n + 2)) // 6
 
# Driver code
N = 5
 
print(sumOfSumSeries(N))
 
# This code is contributed by divyamohan123

C#

// C# program to implement the
// above approach
using System;
class GFG{
 
// Function to find the sum
static long sumOfSumSeries(int n)
{
    return (n * (n + 1) * (n + 2)) / 6;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 5;
     
    Console.Write(sumOfSumSeries(N));
}
}
 
// This code is contributed by Ritik Bansal

Java脚本


输出:
35

考虑乘法,加法和除法的时间复杂度O(1)需要固定时间。