3

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?

Chen M Ling
  • 275
  • 1
  • 6

2 Answers2

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