📌  相关文章
📜  求系列 1 、 2a 、 3a2 、 4a3 、 5a4 、 … 的 n 项之和

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

求系列 1 、 2a 、 3a2 、 4a3 、 5a4 、 … 的 n 项之和

给定一个系列。 1, 2a, 3a^{2}, 4a^{3}, 5a^{4},.....                        a 的值。求数列前n项的总和

例子:

蛮力方法:

一个简单的方法可以是迭代序列的 N 项并将它们相加以计算 a 的任何值的总和。请按照以下步骤了解该方法:

对于每次迭代:

  1. 计算一个n [ n = 0 ]。
  2. n(n+1) 相乘。
  3. (n+1)* an添加到求和并将 n 加 1。
  4. 重复上述过程n次。

插图:

下面是上述方法的实现:

C++
// C++ implementation for the
// approach
#include 
using namespace std;
 
// Function to calculate
// the sum
void calcSum(int a, int n)
{
  // Edge Cases
  if (n < 0)
  {
    cout << "Invalid Input";
    return;
  }
 
  if (a == 0 || n == 1)
  {
    cout << 1;
    return;
  }
 
  // Initialize the variables
  int Sum = 0;
 
  // Calculate Sum upto N terms
  for(int i = 0; i < n; i++)
  {
    int r = pow(a, (i)) * (i + 1);
    Sum += r;
  }
 
  // Print Sum
  cout << Sum;
}
 
// Driver Code
int main()
{
  int a = 3;
  int n = 4;
   
  // Invoke calcSum function with
  // values of a and n
  calcSum(a, n);
  return 0;
}


Java
// Java implementation for the
// approach
import java.util.*;
 
class GFG{
 
// Function to calculate
// the sum
static void calcSum(int a, int n)
{
   
  // Edge Cases
  if (n < 0)
  {
    System.out.print("Invalid Input");
    return;
  }
 
  if (a == 0 || n == 1)
  {
    System.out.print(1);
    return;
  }
 
  // Initialize the variables
  int Sum = 0;
 
  // Calculate Sum upto N terms
  for(int i = 0; i < n; i++)
  {
    int r = (int) (Math.pow(a, (i)) * (i + 1));
    Sum += r;
  }
 
  // Print Sum
  System.out.print(Sum);
}
 
// Driver Code
public static void main(String[] args)
{
  int a = 3;
  int n = 4;
   
  // Invoke calcSum function with
  // values of a and n
  calcSum(a, n);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 implementation for the
# approach
 
# Function to calculate
# the sum
def calcSum(a, n):
 
    # Edge Cases
    if (n < 0):
        print("Invalid Input")
        return
 
    if (a == 0 or n == 1):
 
        print(1)
        return
 
    # Initialize the variables
    Sum = 0
 
    # Calculate Sum upto N terms
    for i in range(n):
        r = pow(a, (i)) * (i + 1)
        Sum += r
 
    # Print Sum
    print(Sum)
 
# Driver Code
if __name__ == "__main__":
 
    a = 3
    n = 4
 
    # Invoke calcSum function with
    # values of a and n
    calcSum(a, n)
 
    # This code is contributed by ukasp.


C#
// C# program to find GCD of two
// numbers
using System;
using System.Collections;
 
class GFG {
 
// Function to calculate
// the sum
static void calcSum(int a, int n)
{
  // Edge Cases
  if (n < 0)
  {
    Console.Write("Invalid Input");
    return;
  }
 
  if (a == 0 || n == 1)
  {
    Console.Write(1);
    return;
  }
 
  // Initialize the variables
  int Sum = 0;
 
  // Calculate Sum upto N terms
  for(int i = 0; i < n; i++)
  {
    int r = (int)Math.Pow(a, (i)) * (i + 1);
    Sum += r;
  }
 
  // Print Sum
  Console.Write(Sum);
}
 
// Driver method
public static void Main()
{
    int a = 3;
    int n = 4;
   
    // Invoke calcSum function with
    // values of a and n
    calcSum(a, n);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate
// the sum
void calcSum(int a, int n)
{
  // Edge Cases
  if (n < 0)
  {
    cout << "Invalid Input";
    return;
  }
 
  if (a == 0 || n == 1)
  {
    cout << 1;
    return;
  }
 
  // Sum of First N Natural Numbers
  // In case a = 1
  if (a == 1)
  {
    // Avoiding Overflow
    if (n % 2 == 0)
      cout << (n / 2) * (n + 1);
 
    else
      cout << ((n + 1) / 2) * n;
  }
 
  // Calculate Sum with the help
  // of formula
  int r = pow(a, n);
  int d = pow(a - 1, 2);
  int Sum = (1 - r * (1 + n - n * a)) / d;
 
  // Print Sum
  cout << Sum;
}
 
// Driver Code
int main()
{
  int a = 3;
  int n = 4;
   
  // Invoke calcSum function
  // with values of a and n
  calcSum(a, n);
  return 0;
}


Java
// Java program to implement
// the above approach
class GFG {
 
    // Function to calculate
    // the sum
    public static void calcSum(int a, int n)
    {
       
        // Edge Cases
        if (n < 0) {
            System.out.println("Invalid Input");
            return;
        }
 
        if (a == 0 || n == 1) {
            System.out.println(1);
            return;
        }
 
        // Sum of First N Natural Numbers
        // In case a = 1
        if (a == 1) {
            // Avoiding Overflow
            if (n % 2 == 0)
                System.out.println((n / 2) * (n + 1));
 
            else
                System.out.println(((n + 1) / 2) * n);
        }
 
        // Calculate Sum with the help
        // of formula
        int r = (int) Math.pow(a, n);
        int d = (int) Math.pow(a - 1, 2);
        int Sum = (1 - r * (1 + n - n * a)) / d;
 
        // Print Sum
        System.out.println(Sum);
    }
 
    // Driver Code
    public static void main(String args[]) {
        int a = 3;
        int n = 4;
 
        // Invoke calcSum function
        // with values of a and n
        calcSum(a, n);
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python program to implement
# the above approach
 
# Function to calculate
# the sum
def calcSum(a, n):
   
    # Edge Cases
    if (n < 0):
        print("Invalid Input");
        return;
 
    if (a == 0 or n == 1):
        print(1);
        return;
 
    # Sum of First N Natural Numbers
    # In case a = 1
    if (a == 1):
       
        # Avoiding Overflow
        if (n % 2 == 0):
            print((n // 2) * (n + 1));
 
        else:
            print(((n + 1) // 2) * n);
 
    # Calculate Sum with the help
    # of formula
    r =  pow(a, n);
    d = pow(a - 1, 2);
    Sum = (1 - r * (1 + n - n * a)) // d;
 
    # PrSum
    print(Sum);
 
# Driver Code
if __name__ == '__main__':
    a = 3;
    n = 4;
 
    # Invoke calcSum function
    # with values of a and n
    calcSum(a, n);
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
 
using System;
class GFG {
 
    // Function to calculate
    // the sum
    public static void calcSum(int a, int n)
    {
       
        // Edge Cases
        if (n < 0) {
            Console.WriteLine("Invalid Input");
            return;
        }
 
        if (a == 0 || n == 1) {
            Console.WriteLine(1);
            return;
        }
 
        // Sum of First N Natural Numbers
        // In case a = 1
        if (a == 1) {
            // Avoiding Overflow
            if (n % 2 == 0)
                Console.WriteLine((n / 2) * (n + 1));
 
            else
                Console.WriteLine(((n + 1) / 2) * n);
        }
 
        // Calculate Sum with the help
        // of formula
        int r = (int) Math.Pow(a, n);
        int d = (int) Math.Pow(a - 1, 2);
        int Sum = (1 - r * (1 + n - n * a)) / d;
 
        // Print Sum
        Console.WriteLine(Sum);
    }
 
    // Driver Code
    public static void Main() {
        int a = 3;
        int n = 4;
 
        // Invoke calcSum function
        // with values of a and n
        calcSum(a, n);
    }
}
 
// This code is contributed by gfgking.


Javascript


输出:

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

高效的方法

在这种方法中,使用几何级数的概念提出了一种有效的解决方案。几何级数 (GP)中具有第一项 a公比 rn系列的总和为:

让我们使用这个概念来解决问题。

对于 a != 1,系列之和的公式为:

对于 a = 1,级数之和的公式为:
该级数减少为前 n 个自然数的总和,公式变为 -

插图:

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate
// the sum
void calcSum(int a, int n)
{
  // Edge Cases
  if (n < 0)
  {
    cout << "Invalid Input";
    return;
  }
 
  if (a == 0 || n == 1)
  {
    cout << 1;
    return;
  }
 
  // Sum of First N Natural Numbers
  // In case a = 1
  if (a == 1)
  {
    // Avoiding Overflow
    if (n % 2 == 0)
      cout << (n / 2) * (n + 1);
 
    else
      cout << ((n + 1) / 2) * n;
  }
 
  // Calculate Sum with the help
  // of formula
  int r = pow(a, n);
  int d = pow(a - 1, 2);
  int Sum = (1 - r * (1 + n - n * a)) / d;
 
  // Print Sum
  cout << Sum;
}
 
// Driver Code
int main()
{
  int a = 3;
  int n = 4;
   
  // Invoke calcSum function
  // with values of a and n
  calcSum(a, n);
  return 0;
}

Java

// Java program to implement
// the above approach
class GFG {
 
    // Function to calculate
    // the sum
    public static void calcSum(int a, int n)
    {
       
        // Edge Cases
        if (n < 0) {
            System.out.println("Invalid Input");
            return;
        }
 
        if (a == 0 || n == 1) {
            System.out.println(1);
            return;
        }
 
        // Sum of First N Natural Numbers
        // In case a = 1
        if (a == 1) {
            // Avoiding Overflow
            if (n % 2 == 0)
                System.out.println((n / 2) * (n + 1));
 
            else
                System.out.println(((n + 1) / 2) * n);
        }
 
        // Calculate Sum with the help
        // of formula
        int r = (int) Math.pow(a, n);
        int d = (int) Math.pow(a - 1, 2);
        int Sum = (1 - r * (1 + n - n * a)) / d;
 
        // Print Sum
        System.out.println(Sum);
    }
 
    // Driver Code
    public static void main(String args[]) {
        int a = 3;
        int n = 4;
 
        // Invoke calcSum function
        // with values of a and n
        calcSum(a, n);
    }
}
 
// This code is contributed by saurabh_jaiswal.

Python3

# Python program to implement
# the above approach
 
# Function to calculate
# the sum
def calcSum(a, n):
   
    # Edge Cases
    if (n < 0):
        print("Invalid Input");
        return;
 
    if (a == 0 or n == 1):
        print(1);
        return;
 
    # Sum of First N Natural Numbers
    # In case a = 1
    if (a == 1):
       
        # Avoiding Overflow
        if (n % 2 == 0):
            print((n // 2) * (n + 1));
 
        else:
            print(((n + 1) // 2) * n);
 
    # Calculate Sum with the help
    # of formula
    r =  pow(a, n);
    d = pow(a - 1, 2);
    Sum = (1 - r * (1 + n - n * a)) // d;
 
    # PrSum
    print(Sum);
 
# Driver Code
if __name__ == '__main__':
    a = 3;
    n = 4;
 
    # Invoke calcSum function
    # with values of a and n
    calcSum(a, n);
 
# This code is contributed by 29AjayKumar

C#

// C# program to implement
// the above approach
 
using System;
class GFG {
 
    // Function to calculate
    // the sum
    public static void calcSum(int a, int n)
    {
       
        // Edge Cases
        if (n < 0) {
            Console.WriteLine("Invalid Input");
            return;
        }
 
        if (a == 0 || n == 1) {
            Console.WriteLine(1);
            return;
        }
 
        // Sum of First N Natural Numbers
        // In case a = 1
        if (a == 1) {
            // Avoiding Overflow
            if (n % 2 == 0)
                Console.WriteLine((n / 2) * (n + 1));
 
            else
                Console.WriteLine(((n + 1) / 2) * n);
        }
 
        // Calculate Sum with the help
        // of formula
        int r = (int) Math.Pow(a, n);
        int d = (int) Math.Pow(a - 1, 2);
        int Sum = (1 - r * (1 + n - n * a)) / d;
 
        // Print Sum
        Console.WriteLine(Sum);
    }
 
    // Driver Code
    public static void Main() {
        int a = 3;
        int n = 4;
 
        // Invoke calcSum function
        // with values of a and n
        calcSum(a, n);
    }
}
 
// This code is contributed by gfgking.

Javascript


输出:

时间复杂度: O(1)

辅助空间: O(1)