Open In App
Related Articles

Sum of Arithmetic Geometric Sequence

Improve Article
Improve
Save Article
Save
Like Article
Like

In mathematics, an arithmetico–geometric sequence is the result of the term-by-term multiplication of a geometric progression with the corresponding terms of an arithmetic progression. 
 

is an arithmetico–geometric sequence.
Given the value of a(First term of AP), n(Number of terms), d(Common Difference), b(First term of GP), r(Common ratio of GP). The task is find the sum of first n term of the AGP.
Examples: 
 

Input : First term of AP, a = 1, 
        Common difference of AP, d = 1, 
        First term of GP, b = 2, 
        Common ratio of GP r = 2,
        Number of terms, n = 3
Output : 34
Explanation
Sum = 1*2 + 2*22 + 3*23
    = 2 + 8 + 24
    = 34

 

The nth term of an arithmetico–geometric sequence is the product of the n-th term of an arithmetic sequence and the nth term of a geometric one. Arithmetico–geometric sequences arise in various applications, such as the computation of expected values in probability theory. For example Counting Expected Number of Trials until Success.
n-th term of an AGP is denoted by: tn = [a + (n – 1) * d] * (b * rn-1)
Method 1: (Brute Force) 
The idea is to find each term of the AGP and find the sum.
Below is the implementation of this approach:
 

C++




// CPP Program to find the sum of first n terms.
#include<bits/stdc++.h>
using namespace std;
 
// Return the sum of first n term of AGP
int sumofNterm(int a, int d, int b, int r, int n)
{     
    // finding the each term of AGP and adding
    // it to sum.
    int sum = 0;
    for (int i = 1; i <= n ; i++)   
        sum += ((a + (i -1) * d) * (b * pow(r, i - 1))); 
    return sum;
}
 
// Driven Program
int main()
{
    int a = 1, d = 1, b = 2, r = 2, n = 3;
    cout << sumofNterm(a, d, b, r, n) << endl;
    return 0;
}


Java




// Java Program to find the sum of first n terms.
import java.io.*;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {     
        // finding the each term of AGP and adding
        // it to sum.
        int sum = 0;
        for (int i = 1; i <= n ; i++)   
            sum += ((a + (i -1) * d) * (b * Math.pow(r, i - 1))); 
        return sum;
    }
     
    
    // Driven Program
    public static void main(String args[])
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        System.out.println(sumofNterm(a, d, b, r, n));
         
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3




# Python3 code to find the
# sum of first n terms.
import math
 
# Return the sum of first
# n term of AGP
def sumofNterm( a , d , b ,
                    r , n ):
    # finding the each term
    # of AGP and adding it to sum.
    sum = 0
    for i in range(1,n+1):
        sum += ((a + (i -1) * d) *
            (b * math.pow(r, i - 1)))
    return int(sum)
 
# Driven Code
a = 1
d = 1
b = 2
r = 2
n = 3
print(sumofNterm(a, d, b, r, n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#




// C# Program to find the sum of first n terms.
using System;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        // Finding the each term of AGP
        // and adding it to sum.
        int sum = 0;
        for (int i = 1; i <= n ; i++)
            sum += (int)((a + (i -1) * d) *
                         (b * Math.Pow(r, i - 1)));
        return sum;
    }
     
     
    // Driver Code
    public static void Main()
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        Console.Write(sumofNterm(a, d, b, r, n));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find the
// sum of first n terms.
 
// Return the sum of first
// n term of AGP
function sumofNterm($a, $d, $b, $r, $n)
{
     
    // finding the each term
    // of AGP and adding
    // it to sum.
    $sum = 0;
    for ($i = 1; $i <= $n ; $i++)
        $sum += (($a + ($i -1) * $d) *
                 ($b * pow($r, $i - 1)));
    return $sum;
}
 
// Driver Code
$a = 1; $d = 1; $b = 2; $r = 2; $n = 3;
echo(sumofNterm($a, $d, $b, $r, $n));
 
// This code is contributed by Ajit.
?>


Javascript




<script>
// javascript  program to find the
// sum of first n terms.
 
// Return the sum of first
// n term of AGP
function sumofNterm(a, d, b, r, n)
{
     
    // finding the each term
    // of AGP and adding
    // it to sum.
    let sum = 0;
    for (let i = 1; i <= n ; i++)
        sum += ((a + (i -1) * d) *
                (b * Math.pow(r, i - 1)));
    return sum;
}
 
// Driver Code
let a = 1;
let d = 1;
let b = 2;
let r = 2;
let n = 3;
document.write(sumofNterm(a, d, b, r, n));
 
// This code is contributed by sravan kumar (vignan)
</script>


Output: 
 

34

Time Complexity: O(nlogn) since using a inbuilt pow function inside a loop

Auxiliary Space: O(1) as using constant variables

 
Method 2: (Using Formula) 
 

Proof, 
 

Series,
Sn = ab + (a+d)br + (a+2d)br2 + ..... + (a + (n-1)d)brn-1

Multiplying Sn by r,
rSn = abr + (a+d)br2 + (a+2d)br3 + ..... + (a + (n-1)d)brn

Subtract rSn from Sn,
(1 - r)Sn = [a + (a + d)r + (a + 2d)r2 + ...... + [a + (n-1)d]rn-1] 
           - [ar + (a + d)r2 + (a + 2d)r3 + ...... + [a + (n-1)d]rn]
          = b[a + d(r + r2 + r3 + ...... + rn-1) 
            - [a + (n-1)d]rn]
          (Using sum of geometric series Sn  = a(1 - rn-1)/(1-r))
          = b[a + dr(1 - rn-1)/(1-r) - [a + (n-1)d]rn]

Below is the implementation of this approach: 
 

CPP




// CPP Program to find the sum of first n terms.
#include<bits/stdc++.h>
using namespace std;
 
// Return the sum of first n term of AGP
int sumofNterm(int a, int d, int b, int r, int n)
{
    int ans = 0;   
    ans += a;   
    ans += ((d * r * (1 - pow(r, n-1)))/(1-r));   
    ans -= (a + (n-1)*d)*pow(r, n);   
    return (ans*b)/(1-r);
}
 
// Driven Program
int main()
{
    int a = 1, d = 1, b = 2, r = 2, n = 3;
    cout << sumofNterm(a, d, b, r, n) << endl;
    return 0;
}


Java




// Java Program to find the sum of first n terms.
 
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        int ans = 0;   
        ans += a;   
        ans += ((d * r * (1 - (int)(Math.pow(r, n-1))))/(1-r));   
        ans -= (a + (n-1)*d)*(int)(Math.pow(r, n));   
        return (ans*b)/(1-r);
    }
      
    
    // Driven Program
    public static void main(String args[])
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        System.out.println(sumofNterm(a, d, b, r, n));
         
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3




# Python3 code to find
# the sum of first n terms.
import math
 
# Return the sum of
# first n term of AGP
def sumofNterm( a , d , b ,
                    r , n ):
                         
    ans = 0
    ans += a
     
    ans += ((d * r * (1 - math.pow(r, n-1))
                                )/(1-r))
     
    ans -= (a + (n-1)*d)*math.pow(r, n)
     
    return int((ans*b)/(1-r))
 
# Driven Code
a = 1
d = 1
b = 2
r = 2
n = 3
print(sumofNterm(a, d, b, r, n) )
 
# This code is contributed by "Sharad_Bhardwaj".


C#




// C# Program to find the sum of first n terms.
using System;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        int ans = 0;
        ans += a;
        ans += ((d * r * (1 - (int)(Math.Pow(r, n-1))))
                                               / (1-r));
        ans -= (a + (n-1) * d) *
                (int)(Math.Pow(r, n));
         
        return (ans * b) / (1 - r);
    }
     
     
    // Driver Code
    public static void Main()
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        Console.Write(sumofNterm(a, d, b, r, n));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find the
// sum of first n terms.
 
// Return the sum of first
// n term of AGP
function sumofNterm($a, $d, $b, $r, $n)
{
     
    // finding the each term
    // of AGP and adding
    // it to sum.
    $sum = 0;
    for ($i = 1; $i <= $n ; $i++)
        $sum += (($a + ($i -1) * $d) *
                 ($b * pow($r, $i - 1)));
    return $sum;
}
 
// Driver Code
$a = 1; $d = 1; $b = 2; $r = 2; $n = 3;
echo(sumofNterm($a, $d, $b, $r, $n));
 
// This code is contributed by Ajit.
?>


Javascript




<script>
 
// JavaScript Program to find the sum of first n terms.
 
    // Return the sum of first n term of AGP
    function sumofNterm(a, d, b, r, n)
    {
        let ans = 0;   
        ans += a;   
        ans += ((d * r * (1 - (Math.pow(r, n-1))))/(1-r));   
        ans -= (a + (n-1)*d)*(Math.pow(r, n));   
        return (ans*b)/(1-r);
    }
 
// Driver code   
          
        let a = 1, d = 1, b = 2, r = 2, n = 3;
        document.write(sumofNterm(a, d, b, r, n)); 
             
</script>


Output: 

34

Time Complexity: O(logn) 

Auxiliary Space: O(1)


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 07 Aug, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials