📌  相关文章
📜  系列2和(2 + 4)+(2 + 4 + 6)+(2 + 4 + 6 + 8)+……+(2 + 4 + 6 + 8 +…。+ 2n)的总和

📅  最后修改于: 2021-05-07 01:40:50             🧑  作者: Mango

给定正整数n 。问题是找到给定序列2 +(2 + 4)+(2 + 4 + 6)+(2 + 4 + 6 + 8)+……+(2 + 4 + 6 + 8 +…的总和。 。+ 2n) ,其中系列中的第i个项是第一个i个偶数之和。
例子:

Input : n = 2
Output : 8
(2) + (2+4) = 8

Input : n = 5
Output : 70
(2) + (2+4) + (2+4+6) + (2+4+6+8) + (2+4+6+8+10) = 70

天真的方法:使用两个循环获得每个第i个项的总和,然后将这些总和加到最终总和上。

C++
// C++ implementation to find the sum
// of the given series
#include 
  
using namespace std;
  
// function to find the sum
// of the given series
int sumOfTheSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++) {
  
        // first term of each i-th term
        int k = 2;
        for (int j = 1; j <= i; j++) {
            sum += k;
  
            // next term
            k += 2;
        }
    }
  
    // required sum
    return sum;
}
  
// Driver program to test above
int main()
{
    int n = 5;
    cout << "Sum = "
         << sumOfTheSeries(n);
    return 0;
}


Java
// Java implementation to find the
// sum of the given series
class GFG{
 
    // function to find the sum
    // of the given series
    static int sumOfTheSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
     
            // first term of each i-th term
            int k = 2;
            for (int j = 1; j <= i; j++) {
                sum += k;
     
                // next term
                k += 2;
            }
        }
     
        // required sum
        return sum;
    }
     
    // Driver program to test above
    public static void main(String[] args)
    {
        int n = 5;
 
        System.out.printf("Sum = %d",
                     sumOfTheSeries(n));
    }
}
 
// This code is contriubted by
// Smitha Dinesh Semwal


Python3
# Python3 implementation to find
# the sum of the given series
 
# function to find the sum
# of the given series
def sumOfTheSeries(n):
 
    sum = 0
    for i in range(0, n + 1):
        # first term of each i-th
        # term
        k = 2
        for j in range(1, i + 1):
            sum = sum + k;
 
            # next term
            k = k + 2
     
    # required sum
    return sum;
 
# Driver program to test above
n = 5
ans = sumOfTheSeries(n);
print (ans)
 
# This code is contributed by saloni1297.


C#
// C# implementation to find the
// sum of the given series
using System;
 
class GFG{
 
    // function to find the sum
    // of the given series
    static int sumOfTheSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
     
            // first term of each i-th term
            int k = 2;
            for (int j = 1; j <= i; j++) {
                sum += k;
     
                // next term
                k += 2;
            }
        }
     
        // required sum
        return sum;
    }
     
    // Driver program to test above
    public static void Main()
    {
        int n = 5;
 
        Console.Write("Sum = "+
                    sumOfTheSeries(n));
    }
}
 
// This code is contriubted by
// vt_m


PHP


Javascript


C++
// C++ implementation to find the sum
// of the given series
#include 
using namespace std;
  
// functionn to find the sum
// of the given series
int sumOfTheSeries(int n)
{
    // sum of 1st n natural numbers
    int sum_n = (n * (n + 1) / 2);
     
    // sum of squares of 1st n natural numbers
    int sum_sq_n = (n * (n + 1) / 2) *
                      (2 * n + 1) / 3;
                   
    // required sum
    return (sum_n + sum_sq_n);
}
  
// Driver program to test above
int main()
{
    int n = 5;
    cout << "Sum = "
         << sumOfTheSeries(n);
    return 0;
}


Java
// Java implementation to find the
// sum of the given series
class GFG{
     
    // functionn to find the sum
    // of the given series
    static int sumOfTheSeries(int n)
    {
         
        // sum of 1st n natural numbers
        int sum_n = (n * (n + 1) / 2);
         
        // sum of squares of 1st n natural
        // numbers
        int sum_sq_n = (n * (n + 1) / 2) *
                        (2 * n + 1) / 3;
                     
        // required sum
        return (sum_n + sum_sq_n);
    }
     
    // Driver program to test above
    public static void main(String[] args)
    {
        int n = 5;
         
        System.out.printf("Sum = %d",
                    sumOfTheSeries(n));
    }
}
 
// This code is contriubted by
//Smitha Dinesh Semwal


Python3
# Python3 implementation to find
# the sum of the given series
 
# functionn to find the sum
# of the given series
def sumOfTheSeries(n):
 
    # sum of 1st n natural numbers
    sum_n = int((n * (n + 1) / 2));
     
    # sum of squares of 1st n natural numbers
    sum_sq_n = int ((n * (n + 1) / 2) * (2 * n + 1) / 3)
                     
    # required sum
    return (sum_n + sum_sq_n);
 
# Driver program to test above
n = 5
ans = sumOfTheSeries(n)
print (ans)
 
# This code is contributed by saloni1297.


C#
// C# implementation to find the
// sum of the given series
using System;
 
class GFG{
     
    // functionn to find the sum
    // of the given series
    static int sumOfTheSeries(int n)
    {
         
        // sum of 1st n natural numbers
        int sum_n = (n * (n + 1) / 2);
         
        // sum of squares of 1st n
        // natural numbers
        int sum_sq_n = (n * (n + 1) / 2) *
                        (2 * n + 1) / 3;
                     
        // required sum
        return (sum_n + sum_sq_n);
    }
     
    // Driver program to test above
    public static void Main()
    {
        int n = 5;
         
        Console.Write("Sum = "+
                    sumOfTheSeries(n));
    }
}
 
// This code is contriubted by
// vt_m


PHP


Javascript


输出:

Sum = 70

高效方法:
n为给定级数的第n个项。

an = (2 + 4 + 6 + 8 +....+ 2n).
   = sum of first n even numbers.
   = n * (n + 1).
   = n2 + n.

有关以上公式的证明,请参阅此帖子。
现在,

有关以上公式的证明,请参考此和这篇文章。

C++

// C++ implementation to find the sum
// of the given series
#include 
using namespace std;
  
// functionn to find the sum
// of the given series
int sumOfTheSeries(int n)
{
    // sum of 1st n natural numbers
    int sum_n = (n * (n + 1) / 2);
     
    // sum of squares of 1st n natural numbers
    int sum_sq_n = (n * (n + 1) / 2) *
                      (2 * n + 1) / 3;
                   
    // required sum
    return (sum_n + sum_sq_n);
}
  
// Driver program to test above
int main()
{
    int n = 5;
    cout << "Sum = "
         << sumOfTheSeries(n);
    return 0;
}

Java

// Java implementation to find the
// sum of the given series
class GFG{
     
    // functionn to find the sum
    // of the given series
    static int sumOfTheSeries(int n)
    {
         
        // sum of 1st n natural numbers
        int sum_n = (n * (n + 1) / 2);
         
        // sum of squares of 1st n natural
        // numbers
        int sum_sq_n = (n * (n + 1) / 2) *
                        (2 * n + 1) / 3;
                     
        // required sum
        return (sum_n + sum_sq_n);
    }
     
    // Driver program to test above
    public static void main(String[] args)
    {
        int n = 5;
         
        System.out.printf("Sum = %d",
                    sumOfTheSeries(n));
    }
}
 
// This code is contriubted by
//Smitha Dinesh Semwal

Python3

# Python3 implementation to find
# the sum of the given series
 
# functionn to find the sum
# of the given series
def sumOfTheSeries(n):
 
    # sum of 1st n natural numbers
    sum_n = int((n * (n + 1) / 2));
     
    # sum of squares of 1st n natural numbers
    sum_sq_n = int ((n * (n + 1) / 2) * (2 * n + 1) / 3)
                     
    # required sum
    return (sum_n + sum_sq_n);
 
# Driver program to test above
n = 5
ans = sumOfTheSeries(n)
print (ans)
 
# This code is contributed by saloni1297.

C#

// C# implementation to find the
// sum of the given series
using System;
 
class GFG{
     
    // functionn to find the sum
    // of the given series
    static int sumOfTheSeries(int n)
    {
         
        // sum of 1st n natural numbers
        int sum_n = (n * (n + 1) / 2);
         
        // sum of squares of 1st n
        // natural numbers
        int sum_sq_n = (n * (n + 1) / 2) *
                        (2 * n + 1) / 3;
                     
        // required sum
        return (sum_n + sum_sq_n);
    }
     
    // Driver program to test above
    public static void Main()
    {
        int n = 5;
         
        Console.Write("Sum = "+
                    sumOfTheSeries(n));
    }
}
 
// This code is contriubted by
// vt_m

的PHP


Java脚本


输出:

Sum = 70