0
0
PythonProgramBeginner · 2 min read

Python Program to Find Arithmetic Progression Sum

You can find the sum of an arithmetic progression in Python using the formula sum = n * (2 * a + (n - 1) * d) // 2, where a is the first term, d is the common difference, and n is the number of terms.
📋

Examples

Inputa=1, d=1, n=5
Output15
Inputa=3, d=2, n=4
Output18
Inputa=10, d=0, n=7
Output70
🧠

How to Think About It

To find the sum of an arithmetic progression, first understand that the sequence increases by a fixed amount each time. The sum can be calculated by multiplying the number of terms by the average of the first and last terms. The last term is found by adding the common difference multiplied by (number of terms minus one) to the first term.
📐

Algorithm

1
Get the first term (a), common difference (d), and number of terms (n).
2
Calculate the last term using the formula: last_term = a + (n - 1) * d.
3
Calculate the sum using the formula: sum = n * (a + last_term) / 2.
4
Return or print the sum.
💻

Code

python
def arithmetic_progression_sum(a, d, n):
    last_term = a + (n - 1) * d
    total_sum = n * (a + last_term) // 2
    return total_sum

# Example usage
print(arithmetic_progression_sum(1, 1, 5))  # Output: 15
Output
15
🔍

Dry Run

Let's trace the example where a=1, d=1, n=5 through the code

1

Calculate last term

last_term = 1 + (5 - 1) * 1 = 1 + 4 = 5

2

Calculate sum

total_sum = 5 * (1 + 5) // 2 = 5 * 6 // 2 = 30 // 2 = 15

3

Return sum

return 15

Steplast_termtotal_sum
Calculate last term5
Calculate sum515
Return sum15
💡

Why This Works

Step 1: Find the last term

The last term is found by adding the common difference multiplied by (n - 1) to the first term using last_term = a + (n - 1) * d.

Step 2: Calculate the sum

The sum of the arithmetic progression is the number of terms multiplied by the average of the first and last terms: sum = n * (a + last_term) / 2.

Step 3: Return the result

Return the calculated sum as the final output.

🔄

Alternative Approaches

Using a loop to sum terms
python
def arithmetic_progression_sum_loop(a, d, n):
    total = 0
    for i in range(n):
        total += a + i * d
    return total

print(arithmetic_progression_sum_loop(1, 1, 5))  # Output: 15
This method uses a loop to add each term one by one, which is easier to understand but less efficient for large n.
Using Python's sum and range
python
def arithmetic_progression_sum_range(a, d, n):
    return sum(a + i * d for i in range(n))

print(arithmetic_progression_sum_range(1, 1, 5))  # Output: 15
This uses Python's built-in sum and a generator expression, which is concise but still iterates over all terms.

Complexity: O(1) time, O(1) space

Time Complexity

The formula method calculates the sum using a fixed number of operations regardless of n, so it runs in constant time O(1).

Space Complexity

It uses only a few variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

The formula method is fastest and most efficient. Loop-based methods take O(n) time and are slower for large n.

ApproachTimeSpaceBest For
Formula methodO(1)O(1)Large n, fast calculation
Loop methodO(n)O(1)Small n, simple understanding
Sum with rangeO(n)O(1)Pythonic, readable for small n
💡
Use the formula method for fast calculation, especially with large numbers of terms.
⚠️
Forgetting to subtract 1 from the number of terms when calculating the last term causes incorrect sums.