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?
2 Answers
$\begingroup$ $\endgroup$
1 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!
- $\begingroup$ Yes. That generates the same result. Thanks :) $\endgroup$Chen M Ling– Chen M Ling2016-04-15 11:45:10 +00:00Commented Apr 15, 2016 at 11:45
$\begingroup$ $\endgroup$
1 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.
- $\begingroup$
FoldList[(Flatten@*List), Nothing, Range[10]] == Range@Range[10]$\endgroup$user1066– user10662016-04-15 17:29:28 +00:00Commented Apr 15, 2016 at 17:29
Range /@ Range[10]:-D $\endgroup$Range[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 $\endgroup$Range@Range[10]. See here under 'neat examples' $\endgroup$