📌  相关文章
📜  求系列 3^3 – 2^3, 5^3 – 4^3, 7^3 – 6^3, ... 的 N 项之和

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

求系列 3^3 – 2^3, 5^3 – 4^3, 7^3 – 6^3, ... 的 N 项之和

给定一个正整数N ,任务是找到 该系列的第 N 项总和:

例子

天真的方法

  • 初始化两个 int 变量奇数和偶数。奇数值为 3,偶数值为 2。
  • 现在每次迭代for循环n次将计算当前项并将其添加到总和中。
  • 在每次迭代中,奇数和偶数值都增加 2。
  • 返回结果总和
C++
// C++ program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
#include 
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N)
{
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
        // Calculate the current term
        // and add it to the sum
        Sum += (pow(Odd, 3)
                - pow(Even, 3));
 
        // Increase the odd and
        // even with value 2
        Odd += 2;
        Even += 2;
    }
    return Sum;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findSum(N);
}


Java
// JAVA program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
import java.util.*;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static int findSum(int N)
  {
 
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
      // Calculate the current term
      // and add it to the sum
      Sum += (Math.pow(Odd, 3) - Math.pow(Even, 3));
 
      // Increase the odd and
      // even with value 2
      Odd += 2;
      Even += 2;
    }
    return Sum;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python 3 program for the above approach
 
# Function to calculate the sum
# of first N term
def findSum(N):
    # Initialize the variable
    Odd = 3
    Even = 2
    Sum = 0
 
    # Run a loop for N number of times
    for i in range(N):
 
        # Calculate the current term
        # and add it to the sum
        Sum += (pow(Odd, 3) - pow(Even, 3))
 
        # Increase the odd and
        # even with value 2
        Odd += 2
        Even += 2
         
    return Sum
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 10
     
    # Function call to calculate
    # sum of the series
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.


C#
// C# program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static int findSum(int N)
  {
 
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
      // Calculate the current term
      // and add it to the sum
      Sum += (int)(Math. Pow(Odd, 3) - Math.Pow(Even, 3));
 
      // Increase the odd and
      // even with value 2
      Odd += 2;
      Even += 2;
    }
    return Sum;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 10;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
#include 
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N)
{
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findSum(N);
}


Java
// Java program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
import java.util.*;
 
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return (int) (4 * Math.pow(N, 3) + 9 * Math.pow(N, 2) + 6 * N);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to find the sum of N terms of the
# series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
# Function to calculate the sum
# of first N term
def findSum(N):
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 10
     
    # Function call to calculate
    # sum of the series
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.


C#
// C# program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return 4 * (int)Math.Pow(N, 3)
      + 9 * (int)Math.Pow(N, 2) + 6 * N;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 10;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
4960

时间复杂度:O(N)
辅助空间:O(1)

有效的方法

该序列是通过使用以下模式形成的。

下面是上述方法的实现:

C++

// C++ program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
#include 
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N)
{
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findSum(N);
}

Java

// Java program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
import java.util.*;
 
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return (int) (4 * Math.pow(N, 3) + 9 * Math.pow(N, 2) + 6 * N);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python 3 program to find the sum of N terms of the
# series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
# Function to calculate the sum
# of first N term
def findSum(N):
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 10
     
    # Function call to calculate
    # sum of the series
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.

C#

// C# program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return 4 * (int)Math.Pow(N, 3)
      + 9 * (int)Math.Pow(N, 2) + 6 * N;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 10;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by ukasp.

Javascript


输出
4960

时间复杂度:O(1)
辅助空间:O(1)