# A general term of this sequence [closed]

My question is about the set theory.
I have c[0]={{1}} and i want to define c[n] as follows:

c[n] = Prepend 1 to each element of c[n-1] UNION add 1 to the first element of each element of c[n-1].


For example:

c[1]={{1,1},{2}}

c[2]={{1,1,1},{2,1},{1,2},{3}}

c[3]={{1,1,1,1},{2,1,1},{1,2,1},{3,1},{1,1,2},{2,2},{1,3},{4}}


• Did you search the site for similar questions? Did you find 190310, 126150, 224738? Why do those questions' answers don't answer yours? May 6, 2022 at 14:19

Prepend has an curried form:

Prepend[1] /@ {{a, b}, {c, d, e}}
(* outputs {{1, a, b}, {1, c, d, e}} *)


MapAt can be used to apply a function to parts of a list:

MapAt[1 + # &, {{a, b}, {c, d, e}}, {All, 1}]
(* {{1 + a, b}, {1 + c, d, e}} *)


The function Union does unions. So, we have a pretty straightforward implementation:

c[0] = {{1}};
c[n_] := Union[Prepend[1] /@ c[n - 1], MapAt[1 + # &, c[n - 1], {All, 1}]]


It might be worth adding checks to avoid calculating c[n] with negative or non-integer n. It might also be worth using some memoization. These choices would depend on your context.

• Great Thanks for your help Dear @lericr.
– SKS
May 6, 2022 at 17:01