![]() |
VOOZH | about |
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:
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:
Output:
34
Time Complexity: O(logn)
Auxiliary Space: O(1)