An arithmetic sequence adds the same difference d at every step, like stairs of equal height.
Video lesson · Chapter 1 of 5
A staircase of light
Count gaps instead of rows
Pair the order backwards
Why the loop makes a triangle
Check the order before pricing it
0:00 / 4:24
Arithmetic sequences
1:24·Reel
Read caption
Pia tests each pair among six radio stations and counts fifteen links without repeating one.
One fewer each row still adds to quadratic work.
How it works
Step through the idea, or press Play.
Step 1 of 3
Add the same difference. Every term is the previous one plus a fixed d, so the terms climb (or fall) like equal stairs
Jump straight to any term. Term n is the first term plus n - 1 jumps of d: one line, O(1)
Pair first with last. Columns of first + last all match, so the sum is terms × average, and 0 + 1 + ... + (n - 1) gives O(n²) loops
Understand it
Start here
What an arithmetic sequence is
An arithmetic sequence (also called an arithmetic progression, or AP) is a list of numbers where each term differs from the one before by the same common differenced. In 2,5,8,11,14,… the first term is a1=2 and d=3. Think of climbing stairs where every step is the same height.
The defining rule
ak+1=ak+de.g. 2+35+38+311
One rule fixes the whole sequence once you know where it starts (a1) and how far each step moves (d).
Loop complexity
Nested loops like for j in range(i) run 0+1+⋯+(n−1) times: an arithmetic sum.
O(1) sums
Replace a summing loop with 2n(n+1) and drop from O(n) to O(1).
Array problems
Evenly spaced values, arithmetic slices, and missing terms all lean on the same two formulas.
Formula 1
The nth term
Jump straight to term n
an=a1+(n−1)d
a1 is where you start, n−1 is the number of jumps, and d is the size of each jump. For 2,5,8,…: a10=2+9×3=29.Climbing from a₁ to a₅ in 2, 5, 8, ...
12a₁, 0 jumps
25a₂, 1 jump
38a₃, 2 jumps
411a₄, 3 jumps
514a₅, 4 jumps
Five landings, four stairs. Term $n$ is always $n - 1$ jumps from the start.
Python
defnth_term(a1, d, n):return a1 + (n - 1) * ddefnth_term_slow(a1, d, n): term = a1for _ inrange(n - 1): term += dreturn termprint(nth_term(2, 3, 10)) # 29print(nth_term_slow(2, 3, 10)) # 29, after 9 additionsprint(nth_term(10, -3, 6)) # -5: d can be negativeprint(nth_term(2, 3, 1_000_000)) # 2999999, instantly
The formula is O(1). Walking term by term is O(n).Edit and run it in your browser
Formula 2
The sum: Gauss's pairing trick
The story goes that young Gauss's teacher told the class to add 1 to 100. While the others added one number at a time, Gauss wrote 5050 almost at once. His trick: pair the first number with the last. 1+100=2+99=3+98=101, and there are 50 such pairs.
Pairing first with last
flowchart TD
A["1 + 2 + 3 + ... + 98 + 99 + 100"] --> P1["1 + 100 = 101"]
A --> P2["2 + 99 = 101"]
A --> P3["3 + 98 = 101"]
A --> PD["... 50 pairs in all ..."]
P1 --> T["50 pairs x 101 = 5050"]
P2 --> T
P3 --> T
PD --> T
Moving inward adds 1 to the small number and takes 1 from the big one, so every pair has the same sum.
Sum of the first n terms
Sn=2n(a1+an)=2n(2a1+(n−1)d)
Number of terms times the average of the first and last. For 2,5,…,29: S10=210×31=155. The second form substitutes an=a1+(n−1)d when you do not know the last term.
Quick reference
Formula
Purpose
Example
a1+(n−1)d
nth term
2+9×3=29
2n(a1+an)
Sum of n terms
210×31=155
2n(n+1)
Sum of 1 to n
2100×101=5050
2n(n−1)
Sum of 0 to n - 1
Nested loop count: O(n2)
Derivation
Why the sum formula works
Gauss's pairing works for any arithmetic sequence, odd length included, if you write the sum twice: once forwards and once backwards. Then add the two rows column by column.
Each column sums to a1+an: one step right adds d on top and removes d below. So 2S=n(a1+an) and S=2n(a1+an).
The special case 1 + 2 + ... + n
1+2+⋯+n=2n(n+1)0+1+⋯+(n−1)=2n(n−1)
Set a1=1, an=n for the first; a1=0, an=n−1 for the second. Both are about 2n2.
Complexity
Why nested loops are O(n²)
Python
defcount_pairs(n): count = 0for i inrange(n):for j inrange(i): # runs 0, 1, 2, ..., n - 1 times count += 1return countfor n in [5, 10, 100]:print(n, count_pairs(n), n * (n - 1) // 2)# 5 10 10# 10 45 45# 100 4950 4950
The inner loop runs i times, so the total is 0 + 1 + ... + (n - 1).Edit and run it in your browser
From a loop to a Big O class
flowchart TD
L["for i in range(n): for j in range(i)"] --> C["Inner counts: 0, 1, 2, ..., n-1"]
C --> S["Arithmetic sum: n(n-1)/2"]
S --> E["Expand: n^2/2 - n/2"]
E --> D["Drop the constant 1/2 and the smaller n term"]
D --> O["O(n^2)"]
Recognise the arithmetic sequence of inner counts, sum it, then keep only the fastest-growing term.
Exact count to Big O
i=0∑n−1i=2n(n−1)=2n2−2n=O(n2)
For n=5 that is 10 iterations; for n=1000 it is 499,500. Doubling n roughly quadruples the count.
The triangle is about half the square
n
2n(n−1)
n2
ratio
5
10
25
0.40
10
45
100
0.45
100
4,950
10,000
0.495
1,000
499,500
1,000,000
0.4995
Draw the n×n grid of (i,j) pairs: the loop fills the cells below the diagonal, and the ratio approaches 21.
Pattern spotting
Loop shapes and their sums
Inner loop inside for i in range(n)
Inner counts
Total
Class
for j in range(i)
0,1,…,n−1
2n(n−1)
O(n2)
for j in range(i + 1)
1,2,…,n
2n(n+1)
O(n2)
for j in range(i + 1, n)
n−1,…,1,0
2n(n−1)
O(n2)
for j in range(n)
n,n,…,n
n2
O(n2)
for j in range(0, i, 2)
about 2i
about 4n2
O(n2)
j = 1; while j < i: j *= 2
about log2i
about nlog2n
O(nlogn)
If the inner count rises or falls by a constant each pass, it is an arithmetic sum and the loop is quadratic.
Is this nested loop quadratic?
flowchart TD
Q1{"Does the inner count depend on i?"} -- no --> FIX["n x inner count: n^2 if it is n"]
Q1 -- yes --> Q2{"Does it change by a constant each pass?"}
Q2 -- yes --> AP["Arithmetic sum: about n^2/2, so O(n^2)"]
Q2 -- no --> Q3{"Does it multiply or divide each pass?"}
Q3 -- yes --> NL["Geometric: often O(n log n) or O(n)"]
Q3 -- no --> MORE["Write out the counts and sum them"]
Adding a constant gives an arithmetic sum. Multiplying gives a geometric one, which behaves very differently.
Where the triangle hides
Bubble sort
Pass k compares n−k neighbours: (n−1)+⋯+1=2n(n−1).
Selection sort
Finding the minimum of a shrinking suffix: the same triangle of comparisons.
All pairs
Comparing every pair of n items once gives (2n)=2n(n−1) pairs.
In code
Checking for an arithmetic sequence
Is this array arithmetic?
flowchart TD
A["Fewer than 2 elements?"] -- yes --> T["Yes: trivially arithmetic"]
A -- no --> B["d = arr[1] - arr[0]"]
B --> C{"Every arr[i] - arr[i-1] equals d?"}
C -- yes --> T2["Yes"]
C -- no --> F["No"]
One pass, O(n). If the question allows rearranging the values, sort first: O(nlogn).
Python
defis_arithmetic(arr):iflen(arr) < 2:returnTrue d = arr[1] - arr[0]returnall(arr[i] - arr[i - 1] == d for i inrange(2, len(arr)))defcount_arithmetic_slices(arr): count = curr = 0for i inrange(2, len(arr)):if arr[i] - arr[i - 1] == arr[i - 1] - arr[i - 2]: curr += 1# slices ending here grow by one count += currelse: curr = 0return countprint(is_arithmetic([2, 5, 8, 11])) # Trueprint(count_arithmetic_slices([1, 2, 3, 4])) # 3: [1,2,3], [2,3,4], [1,2,3,4]
Counting arithmetic slices (contiguous runs of 3 or more with one difference) reuses the arithmetic sum.Edit and run it in your browser
Slices in a run of k equal-difference elements
1+2+⋯+(k−2)=2(k−1)(k−2)
A run of k elements holds k−2 slices of length 3, k−3 of length 4, and so on down to one of length k. For k=4: 3 slices.
Edge cases
Negative, zero and huge
Negative d is fine
10,7,4,1,−2,… has d=−3. Both formulas still work: a6=10+5(−3)=−5.
d = 0 is an AP too
4,4,4,4 is arithmetic with d=0; the sum is just n×a1.
Integer overflow
In Java or C++, n(n+1) overflows a 32-bit int past n≈46,340. Widen to 64 bits first.
Safe sum code
Python: n * (n + 1) // 2 stays an exact integer.
Java: (long) n * (n + 1) / 2 widens before multiplying.
Or halve whichever of n and n+1 is even, then multiply.
Risky sum code
Python: n * (n + 1) / 2 returns a float and loses precision for huge n.
Java: int arithmetic overflows silently.
(n / 2) * (n + 1) with integer division is wrong when n is odd.
In the wild
Where arithmetic sequences show up
Memory addresses
Element i of an array lives at base+i×size: an arithmetic sequence of addresses.
Pagination
Page p starts at offset (p−1)×pageSize, the nth-term formula exactly.
Linear backoff
Retry after 1 s, 2 s, 3 s, ...: the total wait after k retries is 2k(k+1) seconds.
Handshake counts
n services that each talk to every other need 2n(n−1) connections.
Recap
Key takeaways
Key points
An arithmetic sequence adds the same difference d each step; d can be negative or zero.
nth term: an=a1+(n−1)d. It takes n−1 jumps, not n.
Sum: Sn=2n(a1+an), the number of terms times the average of first and last.
1+2+⋯+n=2n(n+1), so 1+⋯+100=5050 in O(1).
A loop with for j in range(i) runs 2n(n−1) times: O(n2).
Use // in Python and 64-bit integers in Java or C++ for these sums.