## 2018年4月12日木曜日

### 数学 - Python - JavaScript - 無限をかぞえる - 集合論へのプレリュード – 可算集合 - 可算集合の性質(2)(可算集合の直積)

1. $\begin{array}{}g\left(m,n\right)\\ =\left(1+2+\dots +\left(m+\left(n-1\right)-1\right)\right)+n\\ =\left(1+2+\dots +\left(m+n-2\right)\right)+n\\ =\frac{\left(m+n-2\right)\left(m+n-2+1\right)}{2}+n\\ =\frac{\left(m+n-1\right)\left(m+n-2\right)}{2}+n\end{array}$

コード(Emacs)

Python 3

#!/usr/bin/env python3

from sympy import pprint, Rational

def g(m, n):
return Rational((m + n - 1) * (m + n - 2), 2) + n

d = {}
for m in range(1, 10):
for n in range(1, m + 1):
d[g(m, n)] = f'g({m}, {n})'
m -= 1

for n in sorted(d):
print(f'{d[n]} = {n}')


$./sample1.py g(1, 1) = 1 g(2, 1) = 2 g(1, 2) = 3 g(3, 1) = 4 g(2, 2) = 5 g(1, 3) = 6 g(4, 1) = 7 g(3, 2) = 8 g(2, 3) = 9 g(1, 4) = 10 g(5, 1) = 11 g(4, 2) = 12 g(3, 3) = 13 g(2, 4) = 14 g(1, 5) = 15 g(6, 1) = 16 g(5, 2) = 17 g(4, 3) = 18 g(3, 4) = 19 g(2, 5) = 20 g(1, 6) = 21 g(7, 1) = 22 g(6, 2) = 23 g(5, 3) = 24 g(4, 4) = 25 g(3, 5) = 26 g(2, 6) = 27 g(1, 7) = 28 g(8, 1) = 29 g(7, 2) = 30 g(6, 3) = 31 g(5, 4) = 32 g(4, 5) = 33 g(3, 6) = 34 g(2, 7) = 35 g(1, 8) = 36 g(9, 1) = 37 g(8, 2) = 38 g(7, 3) = 39 g(6, 4) = 40 g(5, 5) = 41 g(4, 6) = 42 g(3, 7) = 43 g(2, 8) = 44 g(1, 9) = 45$


HTML5

<pre id="output0"></pre>
<label for="m0">m = </label>
<input id="m0" type="number" min="1" step="1" value="1">
<label for="n0">n = </label>
<input id="n0" type="number" min="1" step="1" value="1">

<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample1.js"></script>


JavaScript

let pre0 = document.querySelector('#output0'),
btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
input_m = document.querySelector('#m0'),
input_n = document.querySelector('#n0'),
inputs = [input_m, input_n],
p = (x) => pre0.textContent += x + '\n',
range = (start, end, step=1) => {
let res = [];
for (let i = start; i < end; i += step) {
res.push(i);
}
return res;
};

let g = (m, n) => (m + n - 1) * (m + n - 2) / 2 + n;

let output = () => {
let m = parseInt(input_m.value, 10),
n = parseInt(input_n.value, 10);

p(g(${m},${n}) = \${g(m, n)});
};

inputs.forEach((input) => input.onchange = output);
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';
output();