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 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 commentsGroup

Magic Numbers
- 19 Problems
- 84 Finishers
- Find the 9's Complement
- Bell Number calculator
- Is this number Munchhausen?
- Smith numbers
- Generate a Parasitic Number
- Armstrong Number
- nth Rational Number
- Generate a Parasitic Number
- Compute Fibonacci Number
- Find vampire numbers
- N-th Odious
- Evil Number
- Armstrong Number
- Generalised Hamming Number
- Polite numbers. Politeness.
- Polite numbers. N-th polite number.
- Bell Number calculator
- Narcissistic number ?
- Is this number Munchhausen?
- Compute Fibonacci Number
- Smith numbers
- Find vampire numbers
- Polygonal numbers
- Generate a Parasitic Number
- nth Rational Number
- Kaprekar numbers
- Parasitic numbers
- Determine if a Given Number is a Triangle Number
Problem Recent Solvers130
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!