CODEBHAVYA โ€ข MATHEMATICS

๐Ÿ”บ Mathematical Patterns for Coding

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
1111
2483
39276
4166410
52512515

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

๐Ÿงช 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.

Problem 2: Count loop executions

The inner loop runs 1, 2, 3, 4 and 5 times.

Solution: Total = 1 + 2 + 3 + 4 + 5 = 5ร—6/2 = 15.

Problem 3: Identify a cycle

A status sequence repeats R, G, B. Find the state at index 14.

Solution: 14 % 3 = 2. Index 2 in [R, G, B] is B.

Problem 4: Continue a recurrence

a1 = 1 and an = 2anโˆ’1 + 1. Find a5.

Solution: The terms are 1, 3, 7, 15 and 31.

โœ๏ธ 12. Practice Problems

Solve each problem yourself, then open its solution to verify your method.

1. Find the next term: 6, 10, 14, 18, ...

2. Find the next term: 2, 6, 12, 20, 30, ...

3. How many times does an inner loop execute if it runs i times for i = 1 to 8?

4. A cycle is [0, 1, 2, 3]. Which value appears at index 19?

5. Find the next Fibonacci term: 3, 5, 8, 13, 21, ...

๐Ÿ“ 13. Quick Revision

  • Constant first difference โ†’ arithmetic pattern.
  • Constant ratio โ†’ geometric pattern.
  • Constant second difference often โ†’ quadratic pattern.
  • 1 + 2 + ... + n = n(n+1)/2.
  • The first n odd numbers add to nยฒ.
  • Use index % k to model a repeating cycle of length k.
  • Always identify starting values before applying a recurrence.
  • Pattern recognition can replace repeated computation with a direct formula.

๐Ÿ”บ Mathematical Patterns Practice

Complete 20 questions from easy to hard. Your score, every option, correct answers and explanations appear after final submission.

Start 20-Question Test โ†’