📌  相关文章
📜  求系列 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... 的总和,直到 3N 项

📅  最后修改于: 2022-05-13 01:56:10.580000             🧑  作者: Mango

求系列 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+... 的总和,直到 3N 项

给定一个数字N ,任务是找到以下系列的总和,直到3N项。

例子:

天真的方法:

如果我们清楚地观察,那么我们可以将其划分为一组 3 项,其中 N 没有。的组。

以下步骤可用于解决问题 -

  • 对于每个迭代i ,计算(i^3+i^2+i)
  • 并将计算值添加到sum (最初总和将为0 )。
  • 返回最终总和

下面是上述方法的实现:

C++
// C++ program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
 
#include 
using namespace std;
 
// Function to return the sum
// upto 3Nth term of the series
int seriesSum(int N)
{
    // Initial value of the sum
    int sum = 0;
 
    // Loop to iterate from 1 to N
    for (int i = 1; i <= N; i++)
    {
        // Adding current calculated value
        // to sum
        sum += (pow(i, 3) + pow(i, 2) + i);
    }
 
    // Return the sum upto 3Nth term
    return sum;
}
 
// Driver Code
int main()
{
    // Get the value of N
    int N = 5;
    cout << seriesSum(N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to return the sum
  // upto 3Nth term of the series
  static int seriesSum(int N)
  {
     
    // Initial value of the sum
    int sum = 0;
 
    // Loop to iterate from 1 to N
    for (int i = 1; i <= N; i++)
    {
       
      // Adding current calculated value
      // to sum
      sum += (Math.pow(i, 3) + Math.pow(i, 2) + i);
    }
 
    // Return the sum upto 3Nth term
    return sum;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int N = 5;
    System.out.print(seriesSum(N));
  }
}
 
// This code is contributed by hrithikgarg03188


Python3
# Python code for the above approach
 
# Function to return the sum
# upto 3Nth term of the series
def seriesSum(N):
   
    # Initial value of the sum
    sum = 0;
 
    # Loop to iterate from 1 to N
    for i in range(1, N + 1):
        # Adding current calculated value
        # to sum
        sum += (i ** 3) + (i ** 2) + i;
 
    # Return the sum upto 3Nth term
    return sum;
 
# Driver Code
 
# Get the value of N
N = 5;
print(seriesSum(N));
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to return the sum
  // upto 3Nth term of the series
  static int seriesSum(int N)
  {
     
    // Initial value of the sum
    int sum = 0;
 
    // Loop to iterate from 1 to N
    for (int i = 1; i <= N; i++)
    {
       
      // Adding current calculated value
      // to sum
      sum += ((int)Math.Pow(i, 3) + (int)Math.Pow(i, 2) + i);
    }
 
    // Return the sum upto 3Nth term
    return sum;
  }
 
  // Driver Code
  public static void Main ()
  {
    int N = 5;
    Console.Write(seriesSum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
 
#include 
using namespace std;
 
// Function to return the sum
// upto 3Nth term of the series
int seriesSum(int N)
{
    return N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12;
}
 
// Driver Code
int main()
{
    // Get the value of N
    int N = 5;
    cout << seriesSum(N);
    return 0;
}


Java
// Java program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
import java.util.*;
public class GFG
{
 
  // Function to return the sum
  // upto 3Nth term of the series
  static int seriesSum(int N)
  {
    return N * (N + 1) * (3 * (int)Math.pow(N, 2) + 7 * N + 8) / 12;
  }
 
  // Driver Code
  public static void main(String args[])
  {
 
    // Get the value of N
    int N = 5;
    System.out.print(seriesSum(N));
  }
}
 
// This code is contributed by Samim Hosdsain Mondal.


Python
# Pyhton program to find the sum of the
# series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
# till 3N terms
import math
 
# Function to return the sum
# upto 3Nth term of the series
def seriesSum(N):
 
    return math.floor(N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12)
 
# Driver Code
 
# Get the value of N
N = 5
print(seriesSum(N))
 
# This code is contributed by Samim Hossain Mondal


C#
// C# program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
using System;
class GFG
{
   
// Function to return the sum
// upto 3Nth term of the series
static int seriesSum(int N)
{
    return N * (N + 1) * (3 * (int)Math.Pow(N, 2) + 7 * N + 8) / 12;
}
 
// Driver Code
public static void Main()
{
   
    // Get the value of N
    int N = 5;
    Console.Write(seriesSum(N));
}
}
 
// This code is contributed by Samim Hosdsain Mondal.


Javascript


输出
295

时间复杂度: O(N)

辅助空间: O(1)

有效的方法:

从给定的系列中,找到第3N项的公式:

因此,直到第 3N项的系列之和可以概括为:

C++

// C++ program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
 
#include 
using namespace std;
 
// Function to return the sum
// upto 3Nth term of the series
int seriesSum(int N)
{
    return N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12;
}
 
// Driver Code
int main()
{
    // Get the value of N
    int N = 5;
    cout << seriesSum(N);
    return 0;
}

Java

// Java program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
import java.util.*;
public class GFG
{
 
  // Function to return the sum
  // upto 3Nth term of the series
  static int seriesSum(int N)
  {
    return N * (N + 1) * (3 * (int)Math.pow(N, 2) + 7 * N + 8) / 12;
  }
 
  // Driver Code
  public static void main(String args[])
  {
 
    // Get the value of N
    int N = 5;
    System.out.print(seriesSum(N));
  }
}
 
// This code is contributed by Samim Hosdsain Mondal.

Python

# Pyhton program to find the sum of the
# series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
# till 3N terms
import math
 
# Function to return the sum
# upto 3Nth term of the series
def seriesSum(N):
 
    return math.floor(N * (N + 1) * (3 * pow(N, 2) + 7 * N + 8) / 12)
 
# Driver Code
 
# Get the value of N
N = 5
print(seriesSum(N))
 
# This code is contributed by Samim Hossain Mondal

C#

// C# program to find the sum of the
// series 1^3+1^2+1+2^3+2^2+2+3^3+3^2+3+...
// till 3N terms
using System;
class GFG
{
   
// Function to return the sum
// upto 3Nth term of the series
static int seriesSum(int N)
{
    return N * (N + 1) * (3 * (int)Math.Pow(N, 2) + 7 * N + 8) / 12;
}
 
// Driver Code
public static void Main()
{
   
    // Get the value of N
    int N = 5;
    Console.Write(seriesSum(N));
}
}
 
// This code is contributed by Samim Hosdsain Mondal.

Javascript


输出
295

时间复杂度: O(1)

辅助空间: O(1)