In this challenge, you'll get four different but somewhat related tasks that must be solved in a specific manner. First, I'll explain the tasks, then follows an explanation of how you must solve it.
Your code should for all four tasks take two positive integers as input: n,m
, where n<m
. All tasks must be solved in the same language. The orientation of matrices are optional (n-by-m may be interpreted as "n rows, m columns", or "n columns, m rows").
Task 1:
Create (and output/print) a vector / list consisting of the elements: n, n+1 ... m-1, m
. So, for n=4, m=9
, you should output: 4,5,6,7,8,9
.
Task 2:
Create (and output/print) a matrix / array / list of lists (or equivalent) looking like this:
n, n+1, ... m-1, m
n+1, n+2, ... m-1, m+1
...
n+m, n+m+1, ... 2*m-1, 2*m
For n=4, m=9
you should output:
4, 5, 6, 7, 8, 9
5, 6, 7, 8, 9, 10
...
13, 14, 15, 16, 17, 18
Task 3:
Create (and output/print) an n-by-m multiplication table (on any suitable format). Example for n=4, m=9
:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
5 10 15 20
6 12 18 24
7 14 21 28
8 16 24 32
9 18 27 36
Task 4:
Output / print a vector / list consisting of the elements in the multiplication table from task 3, sorted in ascending order, keeping duplicate values. For n=4, m=9
, you should output: 1, 2, 2, 3, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 9, 10, 12, 12, 12, 14, 15, 16, 16, 18, 18, 20, 21, 24, 24, 27, 28, 32, 36
.
The challenge:
Now, all the tasks above are quite trivial. The real challenge here is that the code for task 2 must start with the code for task 1, the code for task 3 must start with the code for task 2 and the code for task 4 must start with the code for task 3.
To make it more clear:
Suppose the code for Task 1 is (works in Octave):
@(n,m)(n:m)
Then your code for Task 2 could be (works in Octave):
@(n,m)(n:m)+(0:m)'
The code for task Task 3 must be (doesn't work in Octave):
@(n,m)(n:m)+(0:m)'"Code_for_task_3"
And finally, the code for Task 4 must be (doesn't work in Octave):
@(n,m)(n:m)+(0:m)'"Code_for_task_3""Code_for_task_4"
This is code-golf, so the submission with the shortest code for task 4 in each language wins. As always: Explanations are highly encouraged.
>2;
so that the previous task's code is essentially rendered a no-op? \$\endgroup\$0<n<m
or0<=n<m
? \$\endgroup\$