Sub topic 4
Summing an AP: two forms of the formula
This chapter assumes you can find the nth term of an AP, Tn = a + (n-1)d, and that you can solve linear and quadratic equations. Here a is the first term, d is the common difference, n is the number of terms and l is the last term. The sum of the first n terms is written Sn.
Why the formula works
Write the sum forwards and then backwards:
- S = a + (a+d) + (a+2d) + ... + (l-d) + l
- S = l + (l-d) + (l-2d) + ... + (a+d) + a
Add the two lines term by term. Each column adds to a + l, because moving one step up in the first line means moving one step down in the second. There are n columns, so 2S = n(a + l).
This pairing idea is the same one that gives 1 + 2 + ... + 100 = 100 × 101 / 2 = 5050.
The two forms
Substituting l = a + (n-1)d into S = n(a + l)/2 gives the second form.
| Form | Formula | Use it when |
|---|---|---|
| Last-term form | S_n = n/2 × (a + l) | You know the first and last terms |
| Difference form | S_n = n/2 × (2a + (n-1)d) | You know a, d and n, but not l |
The last-term form is the shorter calculation, so find l first if that is easy. The difference form is better when the problem gives you a and d and asks you to solve for n.
def ap_nth(a, d, n):
return a + (n - 1) * d
def ap_sum(a, d, n):
return n * (2 * a + (n - 1) * d) // 2
print(ap_nth(3, 4, 10)) # 39
print(ap_sum(3, 4, 10)) # 210Check this by hand for 3, 7, 11, ... with n = 10. The last term is 39, so S = 10 × (3 + 39) / 2 = 210. Both forms agree.
The integer division // is safe here. If n is even, the product is even. If n is odd, then n-1 is even, so 2a + (n-1)d is even. Either way the product is even when a, d and n are integers.
Counting the terms first
Many CAT questions give a range and not n. For a range with first term a, last term l and step d:
n = (l - a) / d + 1
Example: find the sum of all multiples of 7 between 100 and 200. The first multiple is 105 and the last is 196. Then n = (196 - 105)/7 + 1 = 14, and S = 14 × (105 + 196) / 2 = 7 × 301 = 2107.
Solving for n when the sum is given
If S_n is known, the difference form becomes a quadratic in n.
Example: how many terms of 3, 7, 11, ... add up to 300?
- Write n/2 × (6 + 4(n-1)) = 300, which simplifies to n(2n + 1) = 300.
- Rearrange to 2n² + n - 300 = 0.
- The discriminant is 1 + 2400 = 2401 = 49². So n = (-1 ± 49)/4, which gives n = 12 or n = -12.5.
- Reject the negative root, because n must be a positive integer. So n = 12. Check: 12 × 25 = 300.
When d is negative, both roots can be valid. Take the AP 20, 16, 12, ... with d = -4. Then Sn = n(22 - 2n). Setting Sn = 36 gives n² - 11n + 18 = 0, so n = 2 or n = 9. The sum is 36 after 2 terms. The terms then keep adding positive amounts until they reach 0 at the 6th term. After that they turn negative and bring the total back down to 36 at 9 terms.
The function below automates this. It solves d n² + (2a - d) n - 2·target = 0 and keeps only positive integer roots.
from math import isqrt
def ap_n_for_sum(a, d, target):
"""Return the positive integers n with S_n == target."""
if d == 0:
if a != 0 and target % a == 0 and target // a > 0:
return [target // a]
return []
A, B, C = d, 2 * a - d, -2 * target
disc = B * B - 4 * A * C
if disc < 0:
return []
root = isqrt(disc)
if root * root != disc:
return []
result = set()
for num in (-B + root, -B - root):
if num % (2 * A) == 0 and num // (2 * A) > 0:
result.add(num // (2 * A))
return sorted(result)
print(ap_n_for_sum(3, 4, 300)) # [12]
print(ap_n_for_sum(20, -4, 36)) # [2, 9]Useful consequences
Sum as n times the average. S_n = n × (a + l)/2, and (a + l)/2 is the average of the first and last terms. For an odd number of terms, that average is the middle term. So 5 terms centred on 12 sum to 60, whatever d is.
Each term from the partial sums. For n ≥ 2, Tn = Sn - S{n-1}. Also T1 = S_1.
Recognising an AP from S_n. Expanding the difference form gives
S_n = (d/2) n² + (a - d/2) n
So Sn is a quadratic in n with no constant term. If Sn = p n² + q n, then d = 2p and a = p + q.
Example: if Sn = 3n² + 2n, then d = 6 and a = 5. The terms are 5, 11, 17, ... Check: S2 = 12 + 4 = 16 = 5 + 11.
A word problem
A person saves 100 in the first month and 20 more each month than in the previous month. After how many months does the total saved reach 3600?
Here a = 100 and d = 20. Then n/2 × (200 + 20(n-1)) = 3600, which simplifies to n(90 + 10n) = 3600. Dividing by 10 gives n² + 9n - 360 = 0, which factorises as (n + 24)(n - 15) = 0. So n = 15. Check the last term: 100 + 14 × 20 = 380, and 15 × (100 + 380)/2 = 3600.
Common errors
- Using n in place of n-1 in the difference form. The bracket is 2a + (n-1)d.
- Treating l as given when the question describes a range. Work out the first and last terms that actually belong to the range.
- Using d = 0 in the quadratic solver. An AP with d = 0 is constant, so S_n = n a, and the quadratic degenerates to a linear equation.
- Keeping a negative or fractional root of n.
- For a decreasing AP, assuming the sum keeps growing. The sum can peak and then fall, as in the 20, 16, 12, ... example.
Exercise
Find the sum of all three-digit numbers that are divisible by 6.
Show answer
The first is 102 and the last is 996. n = (996 - 102)/6 + 1 = 150. S = 150 × (102 + 996)/2 = 75 × 1098 = 82350.
Exercise
The sum of the first n terms of an AP is S_n = 2n² + 5n. Find the 20th term.
Show answer
T20 = S20 - S19 = (800 + 100) - (722 + 95) = 900 - 817 = 83. As a check, d = 2p = 4 and a = p + q = 7, so T20 = 7 + 19 × 4 = 83.