3
$\begingroup$

Let n and m be natural numbers. Then, how does one write a function ordint[n,m] that gives every $2n$ integers ${a_1,...,a_n,b_1,...,b_n}$ such that $0\leq a_1$, $a_i < a_j$ if $i < j$, $a_n \leq b_1$, $b_i < b_j$ if $i < j$, and $b_n \leq m$? For example, the following ordint2[m] gives the desired output for n=2.

ordint2[m_]:=Module[{S,A,B},
 S={};
 Do[S=Append[S,{A[1],A[2],B[1],B[2]}],
    {B[2],0,m},{B[1],0,B[2]-1},{A[2],0,B[1]},{A[1],0,A[2]-1}];
 S]

But, I do not know how to implement the varying index condition {B[n],0,m},{B[n-1],0,B[n]-1},....,{B[1],0,B[2]-1},{A[n],0,B[1]},{A[n-1],0,A[n]-1},...,{A[1],0,A[2]-1} to write ordint[n,m] for a general n.

$\endgroup$
3
$\begingroup$

For the start, you can try this one:

ordint[m_Integer?Positive, n_Integer?Positive] := Join @@ Table[
   (* searching for all lists with a[[n]] = k <= b[[1]]*)
   Join[
    (* searching for all lists with a[[n]]= k = b[[1]]*)
    Partition[
     Flatten[
      Outer[
       Join,
       Subsets[Range[0, k - 1], {n - 1}],
       {{k}},
       {{k}},
       Subsets[Range[k + 1, m], {n - 1}],
       1
       ]
      ], 
      2 n],
    (* searching for all lists with a[[n]] = k < b[[1]]*)
    Partition[
     Flatten[
      Outer[
       Join,
       Subsets[Range[0, k - 1], {n - 1}],
       {{k}},
       Subsets[Range[k + 1, m], {n}],
       1
       ]
      ], 
      2 n]
    ],
   {k, n - 1, m - n + 1}]

ordint[3, 2]

{{0, 1, 1, 2}, {0, 1, 1, 3}, {0, 1, 2, 3}, {0, 2, 2, 3}, {1, 2, 2, 3}}

$\endgroup$
  • $\begingroup$ Thank you very much! But, you need to vary k in the code, right? Indeed, I got erros such as "Table::iterb: Iterator {k,1,3-k} does not have appropriate bounds" by Clear[k]; ordint[3,2]. $\endgroup$ – veo Sep 14 '18 at 1:04
  • $\begingroup$ Oops. Fixed it. Please try againg. $\endgroup$ – Henrik Schumacher Sep 14 '18 at 5:35
  • $\begingroup$ Yes, now it gives correct outputs! Thank you. $\endgroup$ – veo Sep 14 '18 at 18:20
  • $\begingroup$ You're welcome. $\endgroup$ – Henrik Schumacher Sep 15 '18 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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