## 2017年4月20日木曜日

### 数学 - JavaScript - 「場合の数」 を数える - 順列・組合せ – 二項定理 - 多項定理(項の種類、同じ型、係数)

1. $\begin{array}{l}\left(\begin{array}{c}4+6-1\\ 6\end{array}\right)=\left(\begin{array}{c}9\\ 6\end{array}\right)=\left(\begin{array}{c}9\\ 3\end{array}\right)=3·4·7=84\\ \\ 6+0+0+0=6,\left(\begin{array}{c}6\\ 0\end{array}\right)=1,{a}^{9}\\ 5+1+0+0=6,\left(\begin{array}{c}6\\ 1\end{array}\right)=6,{a}^{9}b\\ 4+2+0+0=6,\left(\begin{array}{c}6\\ 2\end{array}\right)=15,{a}^{4}{b}^{2}\\ 4+1+1+0=6,\left(\begin{array}{c}6\\ 4\end{array}\right)·\left(\begin{array}{c}2\\ 1\end{array}\right)=30,{a}^{4}bc\\ 3+3+0+0=6,\left(\begin{array}{c}6\\ 3\end{array}\right)=20,{a}^{3}{b}^{3}\\ 3+2+1+0=6,\left(\begin{array}{c}6\\ 3\end{array}\right)·\left(\begin{array}{c}3\\ 2\end{array}\right)=60,{a}^{3}{b}^{2}c\\ 3+1+1+1=6,\left(\begin{array}{c}6\\ 3\end{array}\right)·\left(\begin{array}{c}3\\ 1\end{array}\right)·\left(\begin{array}{c}2\\ 1\end{array}\right)=120,{a}^{3}bcd\\ 2+2+2+0=6,\left(\begin{array}{c}6\\ 2\end{array}\right)·\left(\begin{array}{c}4\\ 2\end{array}\right)=15·6=90,{a}^{2}{b}^{2}{c}^{2}\\ 2+2+1+1=6,\left(\begin{array}{c}6\\ 2\end{array}\right)·\left(\begin{array}{c}4\\ 2\end{array}\right)·\left(\begin{array}{c}2\\ 1\end{array}\right)=180,{a}^{2}{b}^{2}cd\end{array}$

コード(Emacs)

HTML5

<button id="run0">run</button>
<button id="clear0">clear</button>
<pre id="output0"></pre>
<script src="sample46.js"></script>


JavaScript

let btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
pre0 = document.querySelector('#output0'),
p = (x) => pre0.textContent += x + '\n';

let range = (start, end, step=1) => {
let iter = (i, result) => {
return i >= end ? result : iter(i + step, result.concat([i]));
}
return iter(start, []);
};
let factorial = (n) => {
return n <= 1 ? 1 : n * factorial(n - 1);
};

let combination = (n, r) => {
return factorial(n) / (factorial(r) * factorial(n - r));
};

let output = () => {
p('46.');
[
[[9, 3]],
[[6, 0]],
[[6, 1]],
[[6, 2]],
[[6, 4], [2, 1]],
[[6, 3]],
[[6, 3], [3, 2]],
[[6, 3], [3, 1], [2, 1]],
[[6, 2], [4, 2]],
[[6, 2], [4, 2], [2, 1]]
].forEach((args) => {
p(args.reduce((prev, x) => prev * combination(...x), 1));
});
};

btn0.onclick = output;
btn1.onclick = () => {
pre0.textContent = '';
};

output();