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 Solvers128
Suggested Problems
-
25348 Solvers
-
Return a list sorted by number of occurrences
2776 Solvers
-
Sum all integers from 1 to 2^n
15307 Solvers
-
Back to basics 22 - Rotate a matrix
903 Solvers
-
359 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!