Cody

Problem 1771. Polygonal numbers

Solution 2872835

Submitted on 24 Aug 2020 by Ramesh Kumar V
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
assert(P(4,3)==9)

m = 1 y = [] p = 9 y = 9

2   Pass
s=3; n=1:5; y_correct=[1 3 6 10 15]; assert(isequal(P(s,n),y_correct))

m = 5 y = [] p = 1 y = 1 p = 3 y = 1 3 p = 6 y = 1 3 6 p = 10 y = 1 3 6 10 p = 15 y = 1 3 6 10 15

3   Pass
s=3:6; n=4; Y_correct=[10 16 22 28]; assert(isequal(P(s,n),Y_correct))

m = 1 y = [] p = 10 16 22 28 y = 10 16 22 28

4   Pass
s=randi([3 1000],1,10); n=1; y_correct=ones(1,10); assert(isequal(P(s,n),y_correct))

m = 1 y = [] p = 1 1 1 1 1 1 1 1 1 1 y = 1 1 1 1 1 1 1 1 1 1

5   Pass
s=randi(1000,1,10)+2; n=5; y_correct=10*s-15; assert(isequal(P(s,n),y_correct))

m = 1 y = [] p = 1945 9385 6675 6515 2975 6585 8925 1375 1325 1225 y = 1945 9385 6675 6515 2975 6585 8925 1375 1325 1225

6   Pass
s=[5 6 7]'; n=[2 8 9]; y_correct=[5 92 117;6 120 153;7 148 189]; assert(isequal(P(s,n),y_correct))

m = 3 y = [] p = 5 6 7 y = 5 6 7 p = 92 120 148 y = 5 92 6 120 7 148 p = 117 153 189 y = 5 92 117 6 120 153 7 148 189

7   Pass
% for M.Z. s=[5 6 7]'; n=[2 8 9]; y_correct=[5 92 117;6 120 153;7 148 189]; assert(isequal(P(flipud(s),n),flipud(y_correct)))

m = 3 y = [] p = 7 6 5 y = 7 6 5 p = 148 120 92 y = 7 148 6 120 5 92 p = 189 153 117 y = 7 148 189 6 120 153 5 92 117

8   Pass
s=randi([3,1000]); n=randi([3,1000]); assert(P(s+1,n+1)-P(s,n+1)==P(3,n))

m = 1 y = [] p = 99072524 y = 99072524 m = 1 y = [] p = 98970598 y = 98970598 m = 1 y = [] p = 101926 y = 101926

Suggested Problems

More from this Author41

Community Treasure Hunt

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

Start Hunting!