Cody

# Problem 44457. Triangle of numbers

Solution 2608561

Submitted on 24 Jun 2020 by Othman Salih
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
filetext = fileread('triangle.m'); assert(isempty(strfind(filetext, 'regexp')),'regexp hacks are forbidden')

2   Pass
n = 0; mat_correct = []; assert(isequal(triangle(n),mat_correct))

mat = [] x = 1 i = 1

3   Pass
n = 1; mat_correct = 1; assert(isequal(triangle(n),mat_correct))

mat = [] x = 1 i = 1 j = 1 mat = 1 x = 2 j = 2 i = 2

4   Pass
n = 6; mat_correct = [1 0 0; 2 3 0; 4 5 6]; assert(isequal(triangle(n),mat_correct))

mat = [] x = 1 i = 1 j = 1 mat = 1 x = 2 j = 2 i = 2 j = 1 mat = 1 2 x = 3 j = 2 mat = 1 0 2 3 x = 4 j = 3 i = 3 j = 1 mat = 1 0 2 3 4 0 x = 5 j = 2 mat = 1 0 2 3 4 5 x = 6 j = 3 mat = 1 0 0 2 3 0 4 5 6 x = 7 j = 4 i = 4

5   Pass
n = 12; mat_correct = [1 0 0 0; 2 3 0 0; 4 5 6 0; 7 8 9 10; 11 12 0 0]; assert(isequal(triangle(n),mat_correct))

mat = [] x = 1 i = 1 j = 1 mat = 1 x = 2 j = 2 i = 2 j = 1 mat = 1 2 x = 3 j = 2 mat = 1 0 2 3 x = 4 j = 3 i = 3 j = 1 mat = 1 0 2 3 4 0 x = 5 j = 2 mat = 1 0 2 3 4 5 x = 6 j = 3 mat = 1 0 0 2 3 0 4 5 6 x = 7 j = 4 i = 4 j = 1 mat = 1 0 0 2 3 0 4 5 6 7 0 0 x = 8 j = 2 mat = 1 0 0 2 3 0 4 5 6 7 8 0 x = 9 j = 3 mat = 1 0 0 2 3 0 4 5 6 7 8 9 x = 10 j = 4 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 x = 11 j = 5 i = 5 j = 1 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 11 0 0 0 x = 12 j = 2 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 11 12 0 0 x = 13 j = 3 i = 6

6   Pass
n = 50; mat_correct = [1,zeros(1,8); 2:3,zeros(1,7); 4:6,zeros(1,6); 7:10,zeros(1,5); 11:15,zeros(1,4); 16:21,zeros(1,3); 22:28,0,0; ; 29:36,0; 37:45; 46:50,zeros(1,4)]; assert(isequal(triangle(n),mat_correct))

mat = [] x = 1 i = 1 j = 1 mat = 1 x = 2 j = 2 i = 2 j = 1 mat = 1 2 x = 3 j = 2 mat = 1 0 2 3 x = 4 j = 3 i = 3 j = 1 mat = 1 0 2 3 4 0 x = 5 j = 2 mat = 1 0 2 3 4 5 x = 6 j = 3 mat = 1 0 0 2 3 0 4 5 6 x = 7 j = 4 i = 4 j = 1 mat = 1 0 0 2 3 0 4 5 6 7 0 0 x = 8 j = 2 mat = 1 0 0 2 3 0 4 5 6 7 8 0 x = 9 j = 3 mat = 1 0 0 2 3 0 4 5 6 7 8 9 x = 10 j = 4 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 x = 11 j = 5 i = 5 j = 1 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 11 0 0 0 x = 12 j = 2 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 11 12 0 0 x = 13 j = 3 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 11 12 13 0 x = 14 j = 4 mat = 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 11 12 13 14 x = 15 j = 5 mat = 1 0 0 0 0 2 3 0 0 0 4 5 6 0 0 7 8 9 10 0 11 12 13 14 15 x = 16 j = 6 i = 6 j = 1 mat = 1 0 0 0 0 2 3 0 0 0 4 5 6 0 0 7 8 9 10 0 11 12 13 14 15 16 0 0 0 0 x = 17 j = 2 mat = 1 0 0 0 0 2 3 0 0 0 4 5 6 0 0 7 8 9 10 0 11 12 13 14 15 16 17 0 0 0 x = 18 j = 3 mat = 1 0 0 0 0 2 3 0 0 0 4 5 6 0 0 7 8 9 10 0 11 12 13 14 15 16 17 18 0 0 x = 19 j = 4 mat = 1 0 0 0 0 2 3 0 0 0 4 5 6 0 0 7 8 9 10 0 11 12 13 14 15 16 17 18 19 0 x = 20 j = 5 mat = 1 0 0 0 0 2 3 0 0 0 4 5 6 0 0 7 8 9 10 0 11 12 13 14 15 16 17 18 19 20 x = 21 j = 6 mat = 1 0 0 0 0 0 2 3 0 0 0 0 4 5 6 0 0 0 7 8 9 10 0 0 11 12 13 14 15 0 16 17 18 19 20 21 x = 22 j = 7 i = 7 j = 1 mat = 1 0 0 0 0 0 2 3 0 0 0 0 4 5 6 0 0 0 7 8 9 10 0 0 11 12 13 14 15 0 16 17 18 19 20 21 22 0 0 0 0 0 x = 23 j = 2 mat = 1 0 0 0 0 0 2 3 0 0 0 0 4 5 6 0 0 0 7 8 9 10 0 0 11 12 13 14 15 0 16 17 18 19 20 21 22 23 0 0 0 0 x = 24 j = 3 mat = 1 0 0 0 0 0 2 3 0 0 0 0 4 5 6 0 0 0 7 8 9 10 0 0 11 12 13 14 15 0 16 17 18 19 20 21 22 23 24 0 0 0 x = 25 j = 4 mat = 1 0 0 0 0 0 2 3 0 0 0 0 4 5 6 0 0 0 7 8 9 10 0 0 11 12 13 14 15 0 16 17 18 19 20 21 22 23 24 25 0 0 x = 26 j = 5 mat = 1 0 0 0 0 0 2 3 0 0 0 0 4 5 6 0 0 0 7 8 9 10 0 0 11 12 13 14 15 0 16 17 18 19 20 21 22 23 24 25 26 0 x = 27 j = 6 mat = 1 0 0 0 0 0 2 3 0 0 0 0 4 5 6 0 0 0 7 8 9 10 0 0 11 12 13 14 15 0 16 17 18 19 20 21 22 23 24 25 26 27 x = 28 j = 7 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 x = 29 j = 8 i = 8 j = 1 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 29 0 0 0 0 0 0 x = 30 j = 2 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 29 30 0 0 0 0 0 x = 31 j = 3 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 29 30 31 0 0 0 0 x = 32 j = 4 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 29 30 31 32 0 0 0 x = 33 j = 5 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 29 30 31 32 33 0 0 x = 34 j = 6 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 29 30 31 32 33 34 0 x = 35 j = 7 mat = 1 0 0 0 0 0 0 2 3 0 0 0 0 0 4 5 6 0 0 0 0 7 8 9 10 0 0 0 11 12 13 14 15 0 0 16 17 18 19 20 21 0 22 23 24 25 26 27 28 29 30 31 32 33 34 35 x = 36 j = 8 mat = 1 0 0 0 0 0 0 0 2 3 0 0 0 0 0 0 4 5 6 0 0 0 0 0 7 8 9 10 0 0 0 0 11 12 13 14 15 0 0 0 16 17 18 19 20 21 0 0 22 23 24 25 26 27 28 0 29 30 31 32 33 34 35 36 x = 37 j = 9 i = 9 j = 1 mat = 1 0 0 0 0 0 0 0 2 3 0 0 0 0 0 0 4 5 6 0 0 0 0 0 7 8 9 10 0 0 0 0 11 12 13 14 15 0 0 0 16 17 18 19 20 21 0 0 22 23 24 25 26 27 28 0 29 30 31 32 33 34 35 36 37 0 0 0 0 0 0 0 x = 38 j = 2 mat = 1 0 0 0 0 0 0 0 2 3 0 0 0 0 0 0 4 5 6 0 0 0 0 0 7 8 9 10 0 0 0 0 11 12 13 14 15 0 0 0 16 17 18 19 20 21 0 0 22 23 24 25 26 27 28 0 29 30 31 32 33 34 35 36 37 38 0 0 0 0 0 0 x = 39 j = 3 mat = 1 0 0 0 0 0 0 0 2 3 0 0 0 0 0 0 4 5 6 0 0 0 0 0 7 8 9 10 0 0 0 0 11 12 13 14 15 0 0 0 16 17 18 19 20 21 0 0 22 23 24 25 26 27 28 0 29 30 31 32 33 34 35 36 37 38 39 0 0 0 0 0 x = ...

### Community Treasure Hunt

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

Start Hunting!