-1
$\begingroup$

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}}

Please help me with this homework assignment.

$\endgroup$
2
  • $\begingroup$ Welcome to Mma.SE! Your question needs more from your side. Here it's considered helpful and polite to show your own efforts and share your data and code attempts in a well formatted form, so we can quickly see the problem you are facing. What have you tried? Please help us to help you and edit your question accordingly. Also, please take the tour, it will help you understand the site. If you write an excellent question it will inspire great answers. $\endgroup$
    – rhermans
    May 6, 2022 at 14:15
  • $\begingroup$ Did you search the site for similar questions? Did you find 190310, 126150, 224738? Why do those questions' answers don't answer yours? $\endgroup$
    – rhermans
    May 6, 2022 at 14:19

1 Answer 1

1
$\begingroup$

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.