\(\(\( (*\ $RecursionLimit = \($IterationLimit = 
          20\)\ *) \)\(
\)\(
\)\(For[
        i = 0, i < 200, \(i++\), unused[i] = 0]
For[i = 0, 
        i < 16, \(i++\), unused[i] = 1]
\(\(nos = 
        0; \)\(
\)\)
\(lastcells[i11_, 
          i12_, i13_, i14_, i41_, i32_, i23_, i44_, i22_, i33_, i42_, 
          i43_] := {\ 
{i21 = 0, 
i24 = 
                30 - i22 - i23}
If[\ 
              i24 > 15, 
{i21 = 
                  i24 - 15, 
i24 = 
                  15
}
]

While[
              i24 ≥ 
                0, 
{
If[
                    unused[i21] == 1 && unused[i24] == 1 && 
                      i21 ≠ i24, 
{unused[i21] = 
                        0, 
unused[i24] = 
                        0, 
i31 = 
                        30 - i11 - i21 - i41, 
i34 = 
                        30 - i14 - i24 - i44, 
If[
                          unused[i31] == 1 && 
                            unused[i34] == 
                              1, 
{\ 
nos += 1, pos = nos\ 
}]
{unused[i21] = 
                            1, 
unused[i24] = 1}\  (*\ 
                        these\ brackets\ were\ needed\ to\ have\ un[
                            i24]\ work\ *) 
}] \(i24--\), \(i21++\)}]}\)

\(\(row4[i11_, i12_, 
          i13_, i14_, i41_, i32_, i23_, i44_, i22_, 
          i33_] := {
i42 = 
            30 - i12 - i22 - i32, 
 (*\ 
            Print["ENTER ROW4 nos=", nos, " i11=", i11, " i12=", 
              i12, " i13=", i13, " i14=", i14, " i41=", 
              i41, " i32= ", i32, " i23=", i23, " i44=", 
              i44, " i22=", i22, " i33=", i33, " i42=", 
              i42, " i43=", i43]\ *) 
If[
            i42 ≥ 
              0, 
{If[
                unused[i42] ≠ 
                  0, 
{unused[i42] = 
                    0, 
 (*\ \({\)\(\ \)*) 
i43 = 30 - i13 - i23 - i33, 
If[
                    i43 ≥ 0 && 
                      i41 + i42 + i43 + i44 == 
                        30, 
{If[
                        unused[i43] ≠ 
                          0, 
{unused[i43] = 
                            0, 

lastcells[i11, i12, i13, i14, i41, i32, i23, i44, i22, i33, i42, 
                            i43], 

unused[i43] = 1
}]}], 
unused[i42] = 
                    1
}]}]}\)\(
\)\)
\(\(downdiag[i11_, i12_, i13_, i14_, i41_, i32_, 
          i23_] := {\ 
For[i44 = i11 + 1, 
            i44 < 16, \(i44++\), 
If[\ 
              unused[i44] ≠ 
                0, 
{unused[i44] = 
                  0, 
i22 = 0, 
i33 = 
                  30 - i11 - i44, 
If[
                  i33 > 15, 
{i22 = 
                      i33 - 15, 
i33 = 
                      15}, ], 
While[
                  i33 > \(-1\), 
{If[
                      unused[i22] ≠ 0 && unused[i33] ≠ 0 && 
                        i22 ≠ 
                          i33, 
{unused[i22] = 
                          0, 
unused[i33] = 
                          0, 

row4[i11, 
                          i12, i13, i14, i41, i32, i23, i44, i22, 
                          i33], 

unused[
                            i33] = 1, 
unused[i22] = 
                          1
}], 
\(i33--\), 
\(i22++\)
}], 
                unused[i44] = 
                  1
}]]}\)\(
\)\)
\(\(updiag[i11_, i12_, i13_, 
          i14_] := {
For[i41 = i14 + 1, 
            i41 < 16, \(i41++\), 
If[
              unused[i41] ≠ 
                0, 
{unused[i41] = 
                  0, 
i32 = 0, 
i23 = 
                  30 - i41 - i14, 
If[
                  i23 > 15, 
{i32 = 
                      i23 - 15, 
i23 = 
                      15}, ], 
While[
                  i23 > \(-1\), 
{If[
                      unused[i23] ≠ 0 && unused[i32] ≠ 0 && 
                        i23 ≠ 
                          i32, 
{unused[i23] = 
                          0, 
unused[i32] = 
                          0, 

downdiag[
                          i11, i12, i13, i14, i41, i32, 
                          i23], 

unused[
                            i32] = 1, 
unused[i23] = 
                          1
}], 
\(i23--\), \(i32++\)
}], 
                unused[i41] = 
                  1
}]]}\)\(
\)\(
\)\( (*
downdiag[
            nos_] := {row4[nos], 
              Print["downdiag"]}
\(row4[
                nos_] := {lastcells[nos], 
                  Print["row4"]}
\(lastcells[
                    nos_] := {checkboard[nos], 
                      Print["lastcells"]}
\(checkboard[
                        nos_] := {Print[
                          nos], "checkboard"}\)\)\)
*) \)\(
\)\(
\)\) (*\ 
        MAIN\ \(PROGRAM : \ row1\)\ *) 
For[i11 = 0, 
        i11 < 13, \(i11++\), 
{unused[i11] = 
            0, 
For[i12 = 0, 
            i12 < 16, \(i12++\), 
If[
              unused[i12] ≠ 
                0,  (*\ \(\(≠\)\(\ \)\(not\ needed\ in\ \(C++\)\ code\)\), \ here\ and\ in\ 2\ places\ later\ in\ 3  
                  rd\ If\ *) 
{unused[i12] = 
                  0, 
i13 = 0, 
i14 = 
                  30 - i11 - i12, 
If[
                  i14 > 15, 
{i13 = 
                      i14 - 15, 
i14 = 
                      15
}, ], 
While[
                  i14 ≥ 
                    0, 
{If[
                      unused[i14] ≠ 0 && i14 > i11 && 
                        unused[i13] ≠ 0 && 
                        i13 ≠ 
                          i14, 
{unused[i14] = 
                          0, 
unused[i13] = 
                          0, 

updiag[
                          i11, i12, i13, 
                          i14], 

unused[
                            i14] = 1, 
unused[i13] = 
                          1
}], 
\(i13++\), \(i14--\)
}], 
                unused[i12] = 1
, }]], 
          unused[i11] = 1
}]
Print[
        nos]\)\)\)