Recognize numerical structure, express it with formulas and use it
to design efficient loops, array operations and coding solutions.
๐ฏ 1. Learning Objectives
After completing this topic, you should be able to:
Recognize arithmetic, geometric and successive-difference patterns.
Use square, cube, triangular and Fibonacci patterns.
Connect a sequence term with its index and derive a formula.
Use modular arithmetic to identify repeating cycles.
Translate a mathematical pattern into loops and array logic.
Estimate loop executions before writing or analysing code.
๐งฉ 2. What Is a Mathematical Pattern?
A mathematical pattern is a predictable relationship between values,
positions or operations. In coding, identifying that relationship often
replaces repeated trial-and-error with a short formula or algorithm.
Value Pattern
Each term is produced from the previous term.
3, 7, 11, 15, ... (+4)
Position Pattern
The value is calculated directly from its position n.
1, 4, 9, 16, ... an = nยฒ
Repeating Pattern
A fixed cycle repeats after a known number of steps.
A, B, C, A, B, C, ... index mod 3
Structural Pattern
Rows, columns or neighbouring values follow a rule.
Pascal: value = upper-left + upper-right
โ 3. Arithmetic and Geometric Patterns
Arithmetic Pattern
A constant difference d is added each time.
an = a + (n โ 1)d
Example: 5, 8, 11, 14, ... has d = 3.
Geometric Pattern
Every term is multiplied by a constant ratio r.
an = arn โ 1
Example: 2, 6, 18, 54, ... has r = 3.
Coding connection: Arithmetic patterns appear in index
movement, while geometric patterns appear in repeated doubling,
binary growth and divide-and-conquer analysis.
๐ข 4. Square, Cube and Triangular Patterns
Position n
Square nยฒ
Cube nยณ
Triangular n(n+1)/2
1
1
1
1
2
4
8
3
3
9
27
6
4
16
64
10
5
25
125
15
Sum of Odd Numbers
The first n odd numbers always add to nยฒ.
1 + 3 + 5 + ... + (2n โ 1) = nยฒ
Growing Loop Count
Executing 1, then 2, then 3 operations creates a triangular total.
1 + 2 + ... + n = n(n + 1)/2
๐ 5. Successive-Difference Patterns
When the first difference is not constant, calculate differences again.
A constant second difference usually indicates a quadratic pattern.
Sequence: 4, 7, 12, 19, 28, ... First differences: 3, 5, 7, 9, ... Next difference: 11 Next term: 28 + 11 = 39
๐ฑ 6. Fibonacci and Recurrence
A recurrence defines a value using earlier values. The Fibonacci pattern
uses the previous two terms.
Fn = Fnโ1 + Fnโ2, F0 = 0, F1 = 1
first = 0
second = 1
repeat n times:
print first
next = first + second
first = second
second = next
Important: A recurrence must have one or more starting
values. Without a base value, the sequence cannot begin.
๐บ 7. Pascalโs Triangle
Pascalโs Triangle begins and ends each row with 1. Every interior value
is the sum of the two values directly above it.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
The sum of row n is 2n when counting the top as row 0.
Row n contains n + 1 values.
The values represent binomial coefficients.
It is generated naturally with nested loops or dynamic programming.
๐ 8. Repeating Cycles and Modular Patterns
Alternating Values
Use index % 2 to alternate between two states.
even index โ A, odd index โ B
Fixed-Length Cycle
Use index % k for a cycle containing k states.
0, 1, 2, 0, 1, 2, ... โ index % 3
Wrapping an Array
Move to the next position and return to zero after the last item.
next = (current + 1) % size
Checkerboard Pattern
Rows and columns alternate based on the parity of their sum.
(row + column) % 2
INTERACTIVE VISUALIZER
๐งช 9. Coding Pattern Explorer
Choose a pattern and its values. The sequence and explanation appear
only after you click Generate Pattern.
Select a pattern or example, then click Generate Pattern.
๐ป 10. Patterns in Loops and Arrays
Single Loop
A loop from 1 to n usually performs n iterations.
1 + 1 + ... + 1 = n
Growing Nested Loop
If the inner loop runs i times, the total is triangular.
1 + 2 + ... + n = n(n+1)/2
Halving Pattern
Repeatedly halving the input produces logarithmic growth.
n, n/2, n/4, ... โ about logโn steps
Pair Pattern
The number of unordered pairs from n items is triangular.
n(nโ1)/2 pairs
๐งฎ 11. Solved Problems
Problem 1: Find the next term
4, 7, 12, 19, 28, ...
Solution:
Differences are 3, 5, 7 and 9. The next difference is 11,
so the next term is 28 + 11 = 39.