2017年4月20日木曜日

数学 - JavaScript - 確からしさをみる - 確率 – 確率とその基本性質 - 二三の簡単な確率

1. $\frac{2}{6}=\frac{1}{3}$

2. $\frac{3}{6}=\frac{1}{2}$

3. $\frac{4}{6}=\frac{2}{3}$

1. $\frac{6}{36}=\frac{1}{6}$

2. $\frac{6}{36}=\frac{1}{6}$

3. $\frac{1+2+3+4+5+6}{36}=\frac{21}{36}=\frac{7}{12}$

1. $\frac{\left(\begin{array}{c}4\\ 1\end{array}\right)·\left(\begin{array}{c}6\\ 1\end{array}\right)}{\left(\begin{array}{c}10\\ 2\end{array}\right)}=\frac{24}{45}=\frac{8}{15}$

2. $\frac{\left(\begin{array}{c}6\\ 2\end{array}\right)}{\left(\begin{array}{c}10\\ 2\end{array}\right)}=\frac{15}{45}=\frac{1}{3}$

1. $\frac{2·6!}{8!}=\frac{2}{56}=\frac{1}{28}$

2. $\frac{2·7·6!}{8!}=\frac{2}{8}=\frac{1}{4}$

3. $\frac{6·3!·5!}{8!}=\frac{3·2}{8·7}=\frac{3}{28}$

1. $\frac{2!·5!}{7!}=\frac{1}{21}$

2. $\frac{3·2·5!}{7!}=\frac{1}{7}$

3. $\frac{3!·4!}{7!}=\frac{1}{35}$

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample1.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('2-2.');
let result = [];
range(1, 7).forEach(
(x) => range(1, 7).forEach((y) => {
if (x + y === 7) {
result.push([x, y]);
}
}));
p(result);
p(result.length / 36 === 1 / 6);
p('2-3.');
result = [];
range(1, 7).forEach(
(x) => range(1, 7).forEach((y) => {
if (x + y >= 7) {
result.push([x, y]);
}
}));
p(result);
p(result.length / 36 === 7 / 12);
p('3-1.');
p(combination(4, 1) * combination(6, 1) / combination(10, 2) === 8 / 15);
p('3-2.');
p(combination(6, 2) / combination(10, 2) === 1 / 3);
p('4-1.');
p(2 * factorial(6) / factorial(8) === 1 / 28);
p('4-2.');
p(2 * 7 * factorial(6) / factorial(8) === 1 / 4);
p('4-3.');
p(6 * factorial(3) * factorial(5) / factorial(8) === 3 / 28);
p('5-1.');
p(factorial(2) * factorial(5) / factorial(7) === 1 / 21);
p('5-2.');
p(3 * 2 * factorial(5) / factorial(7) === 1 / 7);
p('5-3.');
p(factorial(3) * factorial(4) / factorial(7) === 1 / 35);

};

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

output();