GFG App
Open App Browser
Continue

# Find the sum of n terms of the series 1 , 2a , 3a2 , 4a3 , 5a4 , …

Given a series . and the value of a. Find the sum of the first n term of the series.

Examples:

Input: a = 3, n = 4
Output: 142

Input: a = 5, n = 1
Output: 1

Brute Force Approach:

A simple approach can be iterating N terms of the series and adding them to calculate the sum for any value of a. Follow the steps below to understand the approach:

For each iteration:

1. Calculate an [ n = 0 ].
2. Multiply an with (n+1).
3. Add (n+1)*an to sum and increment n by 1.
4. Repeat the above processes n times.

Illustration:

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

1. an = 30
= 1
2. an * (n+1) = 30 * (0+1)
= 1 * (1)
= 1
3. sum = sum + an * (n+1)
= 0 + 1
= 1
4. Increment n by 1.

Loop 2: The value of a = 3, n = 1, sum = 1

1. an = 31
= 3
2. an * (n+1) = 31 * (1+1)
= 3 * (2)
= 6
3. sum = sum + an * (n+1)
= 1 + 6
= 7
4. Increment value of n by 1.

Loop 3: The value of a = 3, n = 2, sum = 7

1. an = 32
= 9
2. an * (n+1) = 32 * (2+1)
= 9 * (3)
= 27
3. sum = sum + an * (n+1)
= 7 + 27
= 34
4. Increment n by 1.

Loop 4: The value of a = 3, n = 3, sum = 34

1. an = 33
= 27
2. an * (n+1) = 33 * (3+1)
= 27 * (4)
= 108
3. sum = sum + an * (n+1)
= 34 + 108
= 142
4. Increment the value of n by 1.

Below is the implementation of the above approach:

## 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

 

Output:

142

Time Complexity: O(nlogn) since it is using pow function inside a for loop
Auxiliary Space: O(1)

Efficient Approach

In this approach, an efficient solution is proposed using the concept of Geometric progression. The sum of the series of n terms in a Geometric Progression (G.P.) with first term a and common ratio r is given as: Let’s use this concept to reach a solution to the problem.

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 For a != 1 the formula for sum of the series is: For a = 1 the formula for sum of the series is:
The series reduces to sum of first n natural numbers and the formula becomes- Illustration:

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.

Below is the implementation of the above approach:

## 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;       # 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 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

 

Output:

142

Time Complexity: O(logn) since it is using pow function pow(a,n)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.5236616,"math_prob":0.99928766,"size":9739,"snap":"2023-14-2023-23","text_gpt3_token_len":3471,"char_repetition_ratio":0.12449923,"word_repetition_ratio":0.5222025,"special_character_ratio":0.41400555,"punctuation_ratio":0.15103653,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":1.0000027,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48],"im_url_duplicate_count":[null,null,null,null,null,1,null,6,null,3,null,3,null,1,null,3,null,1,null,3,null,3,null,6,null,3,null,3,null,3,null,3,null,2,null,3,null,6,null,3,null,6,null,3,null,3,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-04T06:47:27Z\",\"WARC-Record-ID\":\"<urn:uuid:8702d990-e94a-4f3d-a11e-88cea3acf061>\",\"Content-Length\":\"380822\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ae310c87-fb93-40d4-af74-1c95abde63b9>\",\"WARC-Concurrent-To\":\"<urn:uuid:a77a13f1-cf7b-4d40-8a71-92a7ae61e0c0>\",\"WARC-IP-Address\":\"44.228.100.190\",\"WARC-Target-URI\":\"https://origin.geeksforgeeks.org/find-the-sum-of-n-terms-of-the-series-1-2a-3a2-4a3-5a4/\",\"WARC-Payload-Digest\":\"sha1:PJAGSFCFNGTD7A4FQ2LSMO4DZMVUAD33\",\"WARC-Block-Digest\":\"sha1:3FPHWQ55V5WMIK6IACTTURNTNHQUEXPM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224649518.12_warc_CC-MAIN-20230604061300-20230604091300-00336.warc.gz\"}"}