Skip to main content
minor correction to use built-in function DiagonalMatrix
Source Link
Syed
  • 59.4k
  • 5
  • 40
  • 95
Clear["Global`*"];
f[n_ /; n ∈ PositiveIntegers] := Module[{
    rule = Alternatives @@ (IdentityMatrix[n]*Range[n])DiagonalMatrix@Range@n /. 0 -> Blank[])
    },
   DeleteCases[Permutations[Range[n]], rule]
   ];

f /@ Range[4] // Column

Result:

enter image description here

Clear["Global`*"];
f[n_ /; n ∈ PositiveIntegers] := Module[{
    rule = Alternatives @@ (IdentityMatrix[n]*Range[n]) /. 0 -> Blank[]
    },
   DeleteCases[Permutations[Range[n]], rule]
   ];

f /@ Range[4] // Column

Result:

enter image description here

Clear["Global`*"];
f[n_ /; n ∈ PositiveIntegers] := Module[{
    rule = Alternatives @@ (DiagonalMatrix@Range@n /. 0 -> Blank[])
    },
   DeleteCases[Permutations[Range[n]], rule]
   ];

f /@ Range[4] // Column

Result:

enter image description here

Source Link
Syed
  • 59.4k
  • 5
  • 40
  • 95

Clear["Global`*"];
f[n_ /; n ∈ PositiveIntegers] := Module[{
    rule = Alternatives @@ (IdentityMatrix[n]*Range[n]) /. 0 -> Blank[]
    },
   DeleteCases[Permutations[Range[n]], rule]
   ];

f /@ Range[4] // Column

Result:

enter image description here