# Generating formulae for polygonal numbers (triangular, square, pentagonal...)

### Generating formulae for polygonal numbers (triangular, square, pentagonal...)

The formula for square numbers is easy to remember:
sn = n2
There are also triangular numbers, pentagonal numbers, hexagonal numbers, and in general k-gonal numbers. These have formulae too... for example, triangular numbers:
tn = n (n + 1) / 2
... but they can be hard to remember.

If you see a fact, try to see it as intuitively as possible.

Some formulae are not worth memorizing - it is more worthwhile to grasp the ideas behind the formulae and rederive them when you need them (or look them up.) This blog post shows how to derive the formulae for k-gonal numbers.

Let's define a few terms. I've defined sn and tn above, and I'll define pn as the nth pentagonal number and hn as the nth hexagonal number, but let's generalize... let gk,n = g(k, n) be the nth k-gonal number. For example:
g(3, n) = tn = n (n + 1) / 2
g(4, n) = sn = n2
The task, then, is to find a formula for g(k, n) for general k and n.

Consider the nth k-gonal number. For simplicity of illustration, I will focus on a particular number, but the approach followed here should be followed while keeping the general case in mind. So, specifically, let's take a look at the 4th pentagonal number p4 = g(5, 4):

The idea is to conceptually "cut" (n – 1) specific lines...

... and then "unroll" the structure to form a kind of "bar chart":

The base of the chart is precisely one edge of the original diagram and so has length n = 4 (counting dots, not lines.) There are a couple of ways to find the height of the last bar - you can observe that each bar (not counting the bottom dot) adds (k – 2) = 3 over the bar before it (as marked below,) and there are (n – 1) = 3 nontrivial bars.
Or you can note that each bar is the bottom dot, plus the sum of (k – 2) = 3 edges, each of which has (n – 1) = 3 unique dots, if we don't count the first dot as being part of the edge.
Either way, you come up with the height being:
h = 1 + (k – 2)(n – 1) = 1 + 3(3) = 10

So we know the base and the height of the triangle. How do we find the area? Well, if we double the triangle, we get a rectangle:

The base of the rectangle is n = 4, and the height is:
h + 1 = 2 + (k – 2) (n – 1) = (k – 2) n + (4 – k) = (5 – 2) 4 + (4 – 5) = 11
The area of the triangle is half of the area of the rectangle:
g(k, n) = n ((k – 2) n + (4 – k)) / 2 = 4 ((5 – 2) 4 + (4 – 5)) / 2 = 22
Counting dots confirms that there are, in fact, 22.

So we have a general theorem - let's try it out on a few special cases. We can use the ones we know as a check.

g(3, n) = n ((3 – 2) n + (4 – 3)) / 2 = n (n + 1) / 2 = tn
g(4, n) = n ((4 – 2) n + (4 – 4)) / 2 = n2 = sn
g(5, n) = n ((5 – 2) n + (4 – 5)) / 2 = n (3n – 1) / 2 = pn
g(6, n) = n ((6 – 2) n + (4 – 6)) / 2 = n (2n – 1) = hn
g(7, n) = n ((7 – 2) n + (4 – 7)) / 2 = n (5n – 3) / 2
...
g(k, n) = n ((k – 2) n + (4 – k)) / 2
...

Further checks: g(k, 1) should be 1 for all k, and g(k, 2) should be k for all k. These also check out.