求系列 1 、 2a 、 3a2 、 4a3 、 5a4 、 … 的 n 项之和
给定一个系列。 和a 的值。求数列前n项的总和。
例子:
Input: a = 3, n = 4
Output: 142
Input: a = 5, n = 1
Output: 1
蛮力方法:
一个简单的方法可以是迭代序列的 N 项并将它们相加以计算 a 的任何值的总和。请按照以下步骤了解该方法:
对于每次迭代:
- 计算一个n [ n = 0 ]。
- 将n与(n+1) 相乘。
- 将(n+1)* an添加到求和并将 n 加 1。
- 重复上述过程n次。
插图:
a = 3 and n = 4
Loop will be executed n number of times i.e 4 in this case.
Loop 1: Initially the value of a = 1, n = 0, sum = 0
- an = 30
= 1 - an * (n+1) = 30 * (0+1)
= 1 * (1)
= 1 - sum = sum + an * (n+1)
= 0 + 1
= 1 - Increment n by 1.
Loop 2: The value of a = 3, n = 1, sum = 1
- an = 31
= 3 - an * (n+1) = 31 * (1+1)
= 3 * (2)
= 6 - sum = sum + an * (n+1)
= 1 + 6
= 7 - Increment value of n by 1.
Loop 3: The value of a = 3, n = 2, sum = 7
- an = 32
= 9 - an * (n+1) = 32 * (2+1)
= 9 * (3)
= 27 - sum = sum + an * (n+1)
= 7 + 27
= 34 - Increment n by 1.
Loop 4: The value of a = 3, n = 3, sum = 34
- an = 33
= 27 - an * (n+1) = 33 * (3+1)
= 27 * (4)
= 108 - sum = sum + an * (n+1)
= 34 + 108
= 142 - Increment the value of n by 1.
下面是上述方法的实现:
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
输出:
142
时间复杂度: O(n)
辅助空间: O(1)
高效的方法
在这种方法中,使用几何级数的概念提出了一种有效的解决方案。几何级数 (GP)中具有第一项 a和公比 r的n项系列的总和为:
让我们使用这个概念来解决问题。
Let
Clearly nth term is
. (1)
Multiply both sides with ‘a’, we get,
(2)
Subtracting equation (2) from (1), we get
Clearly this is the Geometric Progression (G.P.) of n terms with first term 1 and common ration a.
G.P. of n terms with first term a and common ratio r is:
Using the above formula, we have
Dividing both sides by (1 – a), we get
Therefore, the sum of the series is
对于 a != 1,系列之和的公式为:
对于 a = 1,级数之和的公式为:
该级数减少为前 n 个自然数的总和,公式变为 -
插图:
For a = 3, n = 4
Since a != 1, therefore use the formula
Substituting the values of a and n in the above formula, we get
S = -20 – (-162)
S = 142
So, the sum of the series with value of a = 3 and n = 4 is 142.
下面是上述方法的实现:
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
输出:
142
时间复杂度: O(1)
辅助空间: O(1)