I have learnt how to generate a sequence of numbers, but now I come across a question whether it is possible to generate e sequence of lists. Say, I would like to generate {1},{1,2},{1,2,3},...,{1,2,3,...,10}. How can I do that?
Asked
Active
Viewed 246 times
3
2 Answers
4
Try this:
Table[Range[i], {i, 1, 10}]
(* {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}, {1, 2, 3, 4,
5, 6}, {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4,
5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}} *)
or this:
Table[Table[k, {k, 1, i}], {i, 1, 10}]
with the same result. Have fun!
Alexei Boulbitch
- 39,397
- 2
- 47
- 96
2
My entry into the obfuscated Mathematica competition for April, 2016.
rangeList[n_Integer?Positive] := NestList[Join[#, {Last[#] + 1}] &, {1}, n - 1]
rangeList[5]
{{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}}
Too bad I'm posting this on April 15 rather than April 1.
m_goldberg
- 107,779
- 16
- 103
- 257
Range /@ Range[10]:-D – Jason B. Apr 15 '16 at 10:41Range[x]will give{1, 2, 3, 4, 5,......x}, and/@is the infix notation forMap. As an example, lookf /@ {1, 2, 3}gives{f[1], f[2], f[3]}. So in the above,Range[10]gives{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, and then I'm just mappingRangeonto that list – Jason B. Apr 15 '16 at 10:47Range@Range[10]. See here under 'neat examples' – user1066 Apr 15 '16 at 17:26