Skip to main content
Add SubstitutionSystem solution
Source Link
user1066
  • 22.7k
  • 3
  • 36
  • 59

Update

Another use for SubstitutionSystem, which I was unaware of before I read this great answer by @azerbajdzan.

SubstitutionSystem[{n_-> n-1}, {4,2},2]//Flatten

(* {4,2,3,1,2,0} *) 

Or

SubstitutionSystem[{n_-> n+1}, {0,2},2]//Flatten//Reverse

(* {4,2,3,1,2,0} *) 
(SubstitutionSystem[{n_->n+1}, {0,2},#]//Flatten//Reverse)&/@Range[2,10]

(* {
    {4,2,3,1,2,0},
    {5,3,4,2,3,1,2,0},
    {6,4,5,3,4,2,3,1,2,0},
    {7,5,6,4,5,3,4,2,3,1,2,0},
    {8,6,7,5,6,4,5,3,4,2,3,1,2,0},
    {9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}, 
    {10,8,9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}, 
    {11,9,10,8,9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}, 
    {12,10,11,9,10,8,9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}
   } *)

Original Answer

(1)

(1)

Update

Another use for SubstitutionSystem, which I was unaware of before I read this great answer by @azerbajdzan.

SubstitutionSystem[{n_-> n-1}, {4,2},2]//Flatten

(* {4,2,3,1,2,0} *) 

Or

SubstitutionSystem[{n_-> n+1}, {0,2},2]//Flatten//Reverse

(* {4,2,3,1,2,0} *) 
(SubstitutionSystem[{n_->n+1}, {0,2},#]//Flatten//Reverse)&/@Range[2,10]

(* {
    {4,2,3,1,2,0},
    {5,3,4,2,3,1,2,0},
    {6,4,5,3,4,2,3,1,2,0},
    {7,5,6,4,5,3,4,2,3,1,2,0},
    {8,6,7,5,6,4,5,3,4,2,3,1,2,0},
    {9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}, 
    {10,8,9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}, 
    {11,9,10,8,9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}, 
    {12,10,11,9,10,8,9,7,8,6,7,5,6,4,5,3,4,2,3,1,2,0}
   } *)

Original Answer

(1)

add recursive method
Source Link
user1066
  • 22.7k
  • 3
  • 36
  • 59

(1)(1)

(NestList[#+1&,{0,2},2]//Flatten//Reverse)

(* {4, 2, 3, 1, 2, 0} *) 

(2)(2)

(NestList[#+1&,{0,2},3]//Flatten//Reverse)

(*{5, 3, 4, 2, 3, 1, 2, 0} *) 

(3)(3)

(NestList[#+1&,{0,2},#]//Flatten//Reverse)&/@Range[2,10]

(*
{
 {4, 2, 3, 1, 2, 0}, 
 {5, 3, 4, 2, 3, 1, 2, 0}, 
 {6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {12, 10, 11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}
}
*)

(4) Recursively

If[#1[[-1]]>5, Nothing, #0[Sow[#1]+1]]&[{0,2}]//Reap//Flatten//Reverse

(* {5, 3, 4, 2, 3, 1, 2, 0} *) 

(1)

(NestList[#+1&,{0,2},2]//Flatten//Reverse)

(* {4, 2, 3, 1, 2, 0} *) 

(2)

(NestList[#+1&,{0,2},3]//Flatten//Reverse)

(*{5, 3, 4, 2, 3, 1, 2, 0} *) 

(3)

(NestList[#+1&,{0,2},#]//Flatten//Reverse)&/@Range[2,10]

(*
{
 {4, 2, 3, 1, 2, 0}, 
 {5, 3, 4, 2, 3, 1, 2, 0}, 
 {6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {12, 10, 11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}
}
*)

(1)

(NestList[#+1&,{0,2},2]//Flatten//Reverse)

(* {4, 2, 3, 1, 2, 0} *) 

(2)

(NestList[#+1&,{0,2},3]//Flatten//Reverse)

(*{5, 3, 4, 2, 3, 1, 2, 0} *) 

(3)

(NestList[#+1&,{0,2},#]//Flatten//Reverse)&/@Range[2,10]

(*
{
 {4, 2, 3, 1, 2, 0}, 
 {5, 3, 4, 2, 3, 1, 2, 0}, 
 {6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {12, 10, 11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}
}
*)

(4) Recursively

If[#1[[-1]]>5, Nothing, #0[Sow[#1]+1]]&[{0,2}]//Reap//Flatten//Reverse

(* {5, 3, 4, 2, 3, 1, 2, 0} *) 
Source Link
user1066
  • 22.7k
  • 3
  • 36
  • 59

(1)

(NestList[#+1&,{0,2},2]//Flatten//Reverse)

(* {4, 2, 3, 1, 2, 0} *) 

(2)

(NestList[#+1&,{0,2},3]//Flatten//Reverse)

(*{5, 3, 4, 2, 3, 1, 2, 0} *) 

(3)

(NestList[#+1&,{0,2},#]//Flatten//Reverse)&/@Range[2,10]

(*
{
 {4, 2, 3, 1, 2, 0}, 
 {5, 3, 4, 2, 3, 1, 2, 0}, 
 {6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}, 
 {12, 10, 11, 9, 10, 8, 9, 7, 8, 6, 7, 5, 6, 4, 5, 3, 4, 2, 3, 1, 2, 0}
}
*)