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:  * * * * 
According to those rules we can create polygonal numbers for all regular polygons.
 
Your task: given S and N calculate N-th S-gonal numbers P(S,N)
 
Examples:
- P(4, 3) returns [9] because 3-rd square number is 9,
- P(3, 1:5) returns [1, 3, 6, 10, 15] first 5 triangular numbers,
- P(3:6, 4) returns [10, 16, 22, 28] 4-th triangular, square, pentagonal and hexagonal numbers,
- P([3, 4], [1; 2]) returns [1, 1; 3, 4].
see the test suite for more hints
Solution Stats
Problem Comments
- 
		4 Comments
I think the numbers in the description may be wrong. For hexagonal numbers, shouldn't the progression be [1,6,15,28]?
I believe that test suite number 6 is a little bit unfair, since the difficulty of this problem relies on creating the function that gives the n-th s-gonal number, and not making use of meshgrid or bsxfun.
Remove test case 6 please.. it makes no sense
Explain why. It makes a lot of sense to me, and it's a lot easier (and makes even more educational sense) with 2016b. Just learn the difference between * and .* and ask yourself you need 1st or 2nd.
Solution Comments
Show commentsProblem Recent Solvers131
Suggested Problems
- 
         Find relatively common elements in matrix rows 2107 Solvers 
- 
         Find the peak 3n+1 sequence value 2542 Solvers 
- 
         
         620 Solvers 
- 
         "Low : High - Low : High - Turn around " -- Create a subindices vector 567 Solvers 
- 
         
         186 Solvers 
More from this Author40
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!