Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 ยท Patterns and Sequences

Squares, Triangular Numbers and Fibonacci

Explore three famous number patterns: square numbers, triangular numbers and the Fibonacci sequence, with pictures, rules, and code that generates them.

Not every pattern adds or multiplies by a fixed number. Some of the most beautiful sequences follow other rules. In this lesson you will meet three famous ones: the square numbers, the triangular numbers and the Fibonacci sequence. Each can be understood with a picture, a rule, and a few lines of code.

Square numbers

A square number is what you get when you multiply a whole number by itself: 1ร—1 = 1, 2ร—2 = 4, 3ร—3 = 9. Dots arranged in a square grid show why they have that name.

Text
1       4       9        16
โ—      โ— โ—     โ— โ— โ—    โ— โ— โ— โ—
       โ— โ—     โ— โ— โ—    โ— โ— โ— โ—
               โ— โ— โ—    โ— โ— โ— โ—
                        โ— โ— โ— โ—

The sequence is 1, 4, 9, 16, 25, 36, ... with n-th term nยฒ (read "n squared").

Look at the differences: 3, 5, 7, 9, 11, ... These are the odd numbers. That is a neat fact: each new square adds an L-shaped border of dots, and the border gets longer by 2 each time. So the sum of the first n odd numbers is nยฒ: 1 + 3 + 5 + 7 = 16 = 4ยฒ.

Triangular numbers

Arrange dots in a triangle: 1 dot, then rows of 1 and 2 (3 dots), then rows of 1, 2 and 3 (6 dots).

Text
1      3       6
โ—      โ—       โ—
      โ— โ—     โ— โ—
             โ— โ— โ—

The sequence is 1, 3, 6, 10, 15, 21, .... The n-th triangular number is the sum 1 + 2 + 3 + ... + n. You already know a trick for that sum from the arithmetic sequences lesson:

Text
Tโ‚™ = n ร— (n + 1) / 2

Check: n = 5 gives 5 ร— 6 / 2 = 15. โœ“. The differences between triangular numbers are 2, 3, 4, 5, ..., which count upward by one each time.

Here is a lovely connection: put two copies of the same triangular number together and you get a rectangle, n by n + 1. That is why the formula divides by 2.

Python Generate squares and triangular numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last loop shows a surprise: adding two neighboring triangular numbers always gives a square. For example 3 + 6 = 9 and 6 + 10 = 16.

The Fibonacci sequence

The Fibonacci sequence begins:

Text
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

The rule: each term is the sum of the two terms before it. So 2 = 1 + 1, 3 = 1 + 2, 5 = 2 + 3, 8 = 3 + 5, and so on. It is named after Leonardo of Pisa (Fibonacci), who wrote about it in his 1202 book Liber Abaci using a puzzle about breeding rabbits.

Notice this is a term-to-term rule that needs two previous terms. There is no simple "add the same number" or "multiply by the same number" rule. The differences are 0, 1, 1, 2, 3, 5, ..., which is the Fibonacci sequence again (shifted).

Fibonacci numbers appear in the spiral arrangements of some sunflower seeds and pinecones, where the number of spirals in each direction is often two neighboring Fibonacci numbers. Nature does not follow the rule exactly everywhere, but the pattern shows up often.

flowchart LR
  A["1"] --> C["2"]
  B["1"] --> C
  B --> D["3"]
  C --> D
  C --> E["5"]
  D --> E
  D --> F["8"]
  E --> F

Each term (right) is the sum of the two before it (arrows in).

The golden ratio appears

Divide each Fibonacci number by the one before it: 1/1 = 1, 2/1 = 2, 3/2 = 1.5, 5/3 = 1.667, 8/5 = 1.6, 13/8 = 1.625. The ratios settle toward about 1.618. This number is called the golden ratio, and it equals (1 + โˆš5) / 2.

Python Fibonacci and the golden ratio

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Drawing the three growth rates

Plot all three sequences together to see how differently they grow. Squares grow steadily faster, triangular numbers grow like half a square, and Fibonacci eventually outruns both because it grows by a nearly constant ratio (about 1.618 per step).

Python Compare squares, triangular and Fibonacci

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At n = 12 the squares and Fibonacci meet (both are 144). After that Fibonacci pulls ahead, and by n = 30 it wins by a huge margin.

More patterns to know

  • Cube numbers: 1, 8, 27, 64, ... (nยณ).
  • Powers of 2: 1, 2, 4, 8, 16, ..., a geometric sequence.
  • Prime numbers: 2, 3, 5, 7, 11, 13, ..., which have no simple formula.

Common mistakes

  • Starting Fibonacci with 0 and 1 and mixing the two conventions. Both are used, and both produce the same numbers after the start. We use 1, 1.
  • Calling nยฒ "n times 2". It is n times n.
  • Believing the triangular formula needs division to give a fraction. n(n+1) is always even, so the result is a whole number.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the 7th square number?

    7ยฒ = 7 ร— 7 = 49.
  2. 2. What is the 8th triangular number?

    Tโ‚ˆ = 8 ร— 9 / 2 = 36. Check: 1 + 2 + ... + 8 = 36.
  3. 3. What is the next term in 1, 1, 2, 3, 5, 8, ...?

    Add the previous two terms: 5 + 8 = 13.
  4. 4. The differences between consecutive square numbers 1, 4, 9, 16, ... are:

    4 - 1 = 3, 9 - 4 = 5, 16 - 9 = 7, so the differences are the odd numbers.

Summary

  • Square numbers are nยฒ; their differences are the odd numbers.
  • Triangular numbers are n(n+1)/2, the sum 1 + 2 + ... + n.
  • Each Fibonacci number is the sum of the two before it; ratios of neighbors approach the golden ratio, about 1.618.
  • Different patterns grow at different speeds; plotting them makes the contrast visible.

Next: The Coordinate Plane and Ordered Pairs