← Back to challenges
← Recursion: Happy Number·Hexagonal Grid: Distance →

Centered Polygonal Number

JavaScriptHardmathnumbers

Instructions

The centered polygonal numbers are a family of sequences of 2-dimensional figurate numbers, each formed by a central dot, sorrounded by polygonal layers with a constant number of sides. Each side of a polygonal layer contains one dot more than a side in the previous layer.

|

  • | - Centered triangular numbers | Centered square numbers | Centered pentagonal numbers | Centered hexagonal numbers

In the following table are listed the first 12 terms of the sequences of centered k-polygonal numbers, with k from 3 to 22:

kName01234567891011
3Triangular14101931466485109136166199
4Square151325416185113145181221265
5Pentagonal1616315176106141181226276331
6Hexagonal1719376191127169217271331397
7Heptagonal18224371106148197253316386463
8Octagonal19254981121169225289361441529
9Nonagonal110285591136190253325406496595
10Decagonal1113161101151211281361451551661
11Hendecagonal1123467111166232309397496606727
12Dodecagonal1133773121181253337433541661793
13Tridecagonal1144079131196274365469586716859
14Tetradecagonal1154385141211295393505631771925
15Pentadecagonal1164691151226316421541676826991
16Hexadecagonal11749971612413374495777218811057
17Heptadecagonal118521031712563584776137669361123
18Octadecagonal119551091812713795056498119911189
19Enneadecagonal1205811519128640053368585610461255
20Icosagonal1216112120130142156172190111011321
21Icosihenagonal1226412721131644258975794611561387
22Icosidigonal1236713322133146361779399112111453

As you can see:

  • 6 is the 1st pentagonal number;
  • 16 is the 2nd pentagonal number and the 1st pentadecagonal number;
  • 19 is the 3rd triangular number, the 2nd hexagonal number and the 1st octadecagonal number.

Write a function that takes an integer n as argument and returns its classification as polygonal number:

  • return "0th of all" if n is 1, since it is the 0th term of every centered polygonal number sequence;
  • return a list whose generic element is a string formatted as "{i}th {k}-gonal number" if n is the i-th k-gonal number, in k-ascending order;
  • return false if n is not a k-gonal number for any k > 2.

Examples

isPolygonal(3) ➞ false

isPolygonal(4) ➞ ["1st 3-gonal number"]

isPolygonal(16) ➞ ["2nd 5-gonal number", "1st 15-gonal number"]

Notes

N/A

javascript
Loading editor…
⌘ ↵ to run
Walks through the solution with reasoning and edge cases.