# Wolfram言語™

## 永久式を使って組合せ問題を解く

In[1]:=
```Permanent[\!\(\* TagBox[ RowBox[{"(", "", GridBox[{ { SubscriptBox["a", RowBox[{"1", ",", "1"}]], SubscriptBox["a", RowBox[{"1", ",", "2"}]]}, { SubscriptBox["a", RowBox[{"2", ",", "1"}]], SubscriptBox["a", RowBox[{"2", ",", "2"}]]} }, GridBoxAlignment->{ "Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.7]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}], "", ")"}], Function[BoxForm`e\$, MatrixForm[BoxForm`e\$]]]\)]```
Out[1]=
In[2]:=
```Permanent[\!\(\* TagBox[ RowBox[{"(", "", GridBox[{ { SubscriptBox["a", RowBox[{"1", ",", "1"}]], SubscriptBox["a", RowBox[{"1", ",", "2"}]], SubscriptBox["a", RowBox[{"1", ",", "3"}]]}, { SubscriptBox["a", RowBox[{"2", ",", "1"}]], SubscriptBox["a", RowBox[{"2", ",", "2"}]], SubscriptBox["a", RowBox[{"2", ",", "3"}]]}, { SubscriptBox["a", RowBox[{"3", ",", "1"}]], SubscriptBox["a", RowBox[{"3", ",", "2"}]], SubscriptBox["a", RowBox[{"3", ",", "3"}]]} }, GridBoxAlignment->{ "Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.7]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}], "", ")"}], Function[BoxForm`e\$, MatrixForm[BoxForm`e\$]]]\)]```
Out[2]=

すべての要素が1である行列にPermanentを適用するのはおもしろいが，階乗関数を計算するには非効率的である．

In[3]:=
`Table[Permanent[ConstantArray[1, {n, n}]], {n, 10}]`
Out[3]=

In[4]:=
```sets = {{3, 5, 6, 7}, {3, 7}, {1, 2, 4, 5, 7}, {3}, {1, 3, 6}, {1, 5, 7}, {1, 2, 3, 6}}```
Out[4]=
In[5]:=
```m = Table[If[MemberQ[sets[[i]], j], 1, 0] , {i, 7}, {j, 7}]; m // MatrixForm```
Out[5]//MatrixForm=

の永久式が，この問題の解である．

In[6]:=
`Permanent[m]`
Out[6]=

すべてのタプルを明示的に構築して，答を確かめる．

In[7]:=
`Select[Tuples[sets], DuplicateFreeQ]`
Out[7]=