Problem 1771. Polygonal numbers

The task of Problem 5 is to calculate triangular numbers. By playing with dots we can produce also square numbers like:

                                 * * * *
                     * * *       * * * *
           * *       * * *       * * * *
1: *    4: * *    9: * * *   16: * * * *

or hexagonal numbers:

                                          * * * *
                                         *       *
                         * * *          * * * *   *
                        *     *        * *     *   *
            * *        * * *   *        * * *   * * 
           *   *        *   * *          *   * * *
1: *    6:  * *    15:   * * *       28:  * * * *

&nbsp

According to those rules we can create polygonal numbers for all regular polygons.

&nbsp

Your task: given S and N calculate N-th S-gonal numbers P(S,N)

&nbsp

Examples:

  1. P(4, 3) returns [9] because 3-rd square number is 9,
  2. P(3, 1:5) returns [1, 3, 6, 10, 15] first 5 triangular numbers,
  3. P(3:6, 4) returns [10, 16, 22, 28] 4-th triangular, square, pentagonal and hexagonal numbers,
  4. P([3, 4], [1; 2]) returns [1, 1; 3, 4].

see the test suite for more hints

Solution Stats

46.62% Correct | 53.38% Incorrect
Last Solution submitted on Nov 15, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers128

Suggested Problems

More from this Author40

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!