Polygonal numbers - MATLAB Cody - MATLAB Central

Problem 1771. Polygonal numbers

Difficulty:Rate

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.96% Correct | 53.04% Incorrect
Last Solution submitted on Feb 19, 2025

Problem Comments

Solution Comments

Show comments
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
3

Group

CUP Challenge Image
CUP Challenge
  • 11 Problems
  • 4141 Finishers

Problem Recent Solvers130

Community Treasure Hunt

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

Start Hunting!
Go to top of page