Python Program to Find Geometric Progression Sum
You can find the sum of a geometric progression in Python using the formula
S = a * (r**n - 1) / (r - 1) where a is the first term, r is the common ratio, and n is the number of terms.Examples
Inputa=2, r=3, n=4
Output80
Inputa=5, r=1, n=10
Output50
Inputa=1, r=0.5, n=3
Output1.75
How to Think About It
To find the sum of a geometric progression, first identify the first term
a, the common ratio r, and the number of terms n. If the ratio is 1, the sum is simply a * n. Otherwise, use the formula S = a * (r**n - 1) / (r - 1) to calculate the sum efficiently.Algorithm
1
Get the first term (a), common ratio (r), and number of terms (n) as input2
Check if the common ratio (r) is 13
If r is 1, calculate sum as a multiplied by n4
Otherwise, calculate sum using the formula a * (r^n - 1) / (r - 1)5
Return or print the sumCode
python
def geometric_progression_sum(a, r, n): if r == 1: return a * n return a * (r**n - 1) / (r - 1) # Example usage first_term = 2 common_ratio = 3 num_terms = 4 result = geometric_progression_sum(first_term, common_ratio, num_terms) print(result)
Output
80
Dry Run
Let's trace the example with a=2, r=3, n=4 through the code
1
Input values
a=2, r=3, n=4
2
Check if r equals 1
r=3, so condition is False
3
Calculate sum using formula
sum = 2 * (3**4 - 1) / (3 - 1) = 2 * (81 - 1) / 2 = 2 * 80 / 2 = 80
4
Return sum
sum = 80
| Iteration | Calculation | Result |
|---|---|---|
| 1 | 3**4 | 81 |
| 2 | 81 - 1 | 80 |
| 3 | 2 * 80 / 2 | 80 |
Why This Works
Step 1: Handle ratio equals 1
If the common ratio r is 1, the progression is constant, so the sum is simply a * n.
Step 2: Use geometric sum formula
For other ratios, the sum formula S = a * (r**n - 1) / (r - 1) calculates the total efficiently without looping.
Step 3: Return the result
The function returns the sum as a number, which can be printed or used further.
Alternative Approaches
Using a loop to sum terms
python
def geometric_progression_sum_loop(a, r, n): total = 0 term = a for _ in range(n): total += term term *= r return total print(geometric_progression_sum_loop(2, 3, 4))
This method is simple and intuitive but slower for large n because it sums each term one by one.
Using recursion
python
def geometric_progression_sum_rec(a, r, n): if n == 1: return a return a * r**(n-1) + geometric_progression_sum_rec(a, r, n-1) print(geometric_progression_sum_rec(2, 3, 4))
Recursion works but is less efficient and can cause stack overflow for large n.
Complexity: O(1) time, O(1) space
Time Complexity
The formula calculates the sum in constant time without loops, so it is O(1).
Space Complexity
The program uses a fixed amount of memory for variables, so space complexity is O(1).
Which Approach is Fastest?
The formula approach is fastest and most efficient compared to looping or recursion.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Formula | O(1) | O(1) | Large n, fast calculation |
| Loop | O(n) | O(1) | Small n, easy to understand |
| Recursion | O(n) | O(n) | Learning recursion, small n only |
Use the formula method for fast calculation and the loop method for better understanding.
Forgetting to handle the case when the common ratio is 1, which causes division by zero.