Figurate numbers

O(√n)

Figurate numbers generalise the idea of arranging objects in geometric shapes. Triangular numbers T(k)=k(k+1)/2 count objects in equilateral triangles; square numbers k² count objects in squares; pentagonal numbers P(k)=k(3k-1)/2 count objects in pentagons. This visualiser walks k from 1 upward, marking each figurate value with its colour (green for triangular, violet for square, amber for pentagonal). When a number belongs to multiple families, square beats triangular beats pentagonal, so 1 and 36 show as square. The algorithm stops once all three sequences exceed n.

Numbers
Edit the input and press Play

How it works

  1. Initialize all cells 1..n as neutral
  2. Walk k upward, compute T(k), k², P(k)
  3. Mark each figurate value; square beats triangular beats pentagonal
  4. Stop when all three sequences exceed n

Pseudocode

1figurate(n):                        # O(sqrt(n))2  label[1..n] = "neutral"3  for k = 1, 2, ...:4    tri  = k*(k+1)/25    sq   = k*k6    pent = k*(3*k - 1)/27    if all three > n: break8    if sq   <= n: label[sq]   = "square"9    if tri  <= n and label[tri]  != "square": label[tri]  = "triangular"10    if pent <= n and label[pent] not in {"square","triangular"}: label[pent] = "pentagonal"