## 2017年4月21日金曜日

### Algorithms - JavaScript - 基礎概念 - 数学的な基礎(総和、公式の発見、帰納法による証明、階差数列)

The Art of Computer Programming　Volume 1 Fundamental Algorithms Third Edition 日本語版(Donald E. Knuth (著)、青木 孝 (著)、筧 一彦 (著)、鈴木 健一 (著)、長尾 高弘 (著)、有澤 誠 (その他)、和田 英一 (その他)、ドワンゴ)の第1章(基礎概念)、1.2(数学的な基礎)、演習問題11を取り組んでみる。

1. $\begin{array}{l}n=0,\frac{1}{5}\\ n=1,\frac{1}{5}-\frac{27}{85}=\frac{17-27}{85}=-\frac{10}{85}=-\frac{2}{17}\\ n=2,-\frac{2}{17}+\frac{125}{629}=\frac{-74+125}{629}=\frac{51}{629}=\frac{3}{37}\\ n=3,\frac{3}{37}-\frac{343}{2405}=\frac{195-343}{2405}=-\frac{148}{2405}=-\frac{4}{65}\end{array}$

分母に注目。

$\begin{array}{l}5,17,37,65,···\\ 12,20,28,···\\ {b}_{n}=8n+12\\ n\ge 1\\ {a}_{n}=5+\frac{n\left(12+\left(8\left(n-1\right)+12\right)\right)}{2}\\ =5+\frac{n\left(12+8n+4\right)}{2}\\ =5+4n\left(n+2\right)\\ =4{n}^{2}+8n+5\\ =4{\left(n+1\right)}^{2}+1\end{array}$
$\begin{array}{l}{S}_{n}=\frac{{\left(-1\right)}^{n}\left(n+1\right)}{4{\left(n+1\right)}^{2}+1}\\ \\ {S}_{0}=\frac{1}{5}\\ \\ {S}_{n}={S}_{n-1}+\frac{{\left(-1\right)}^{n}{\left(2n+1\right)}^{3}}{{\left(2n+1\right)}^{4}+4}\\ =\frac{{\left(-1\right)}^{n-1}n}{4{n}^{2}+1}+\frac{{\left(-1\right)}^{n}{\left(2n+1\right)}^{3}}{{\left(2n+1\right)}^{4}+4}\\ =\frac{-{\left(-1\right)}^{n}n\left({\left(2n+1\right)}^{4}+4\right)+\left(4{n}^{2}+1\right){\left(-1\right)}^{n}{\left(2n+1\right)}^{3}}{\left(4{n}^{2}+1\right)\left({\left(2n+1\right)}^{4}+4\right)}\\ ={\left(-1\right)}^{n}\frac{\left(4{n}^{2}+1\right)\left(8{n}^{3}+12{n}^{2}+6n+1\right)-\left(16{n}^{5}+32{n}^{4}+24{n}^{3}+8{n}^{2}+5n\right)}{\left(4{n}^{2}+1\right)\left({\left(2n+1\right)}^{4}+4\right)}\\ ={\left(-1\right)}^{n}\frac{\left(32{n}^{5}+48{n}^{4}+32{n}^{3}+16{n}^{2}+6n+1\right)-\left(16{n}^{5}+32{n}^{4}+24{n}^{3}+8{n}^{2}+5n\right)}{\left(4{n}^{2}+1\right)\left({\left(2n+1\right)}^{4}+4\right)}\\ ={\left(-1\right)}^{n}\frac{16{n}^{5}+16{n}^{4}+8{n}^{3}+8{n}^{2}+n+1}{64{n}^{6}+128{n}^{5}+112{n}^{4}+64{n}^{3}+44{n}^{2}+8n+5}\\ =\frac{{\left(-1\right)}^{n}\left(n+1\right)\left(16{n}^{4}+8{n}^{2}+1\right)}{\left(4{n}^{2}+1\right)\left({\left(2n+1\right)}^{4}+4\right)}\\ =\frac{{\left(-1\right)}^{n}\left(n+1\right){\left(4{n}^{2}+1\right)}^{2}}{\left(4{n}^{2}+1\right)\left({\left(2n+1\right)}^{4}+4\right)}\\ =\frac{{\left(-1\right)}^{n}\left(n+1\right)\left(4{n}^{2}+1\right)}{\left({\left(2n+1\right)}^{4}+4\right)}\\ =\frac{{\left(-1\right)}^{n}\left(n+1\right)\left(4{n}^{2}+1\right)}{16{n}^{4}+32{n}^{3}+24{n}^{2}+8n+5}\\ =\frac{{\left(-1\right)}^{n}\left(n+1\right)}{4{n}^{2}+8n+5}\\ =\frac{{\left(-1\right)}^{n}\left(n+1\right)}{4{\left(n+1\right)}^{2}+1}\end{array}$

コード(Emacs)

HTML5

```<pre id="output0"></pre>
n = <input id="n0" type="number" min="0" step="1" value="10">
<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample11.js"></script>
```

JavaScript

```let input0 = document.querySelector('#n0'),
pre0 = document.querySelector('#output0'),
btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
p = (x) => pre0.textContent += x + '\n',
range = (start, end, step=1) => {
let result = [];

for (let i = start; i < end; i += step) {
result.push(i);
}
return result;
};

let term = (n) =>
Math.pow(-1, n) * Math.pow(2 * n + 1, 3) / (Math.pow(2 * n + 1, 4) + 4),
f = (n) => range(0, n + 1)
.map((i) => term(i))
.reduce((x, y) => x + y),
s = (n) => Math.pow(-1, n) * (n + 1) / (4 * Math.pow(n + 1, 2) + 1);

let output = () => {
let n = parseInt(input0.value, 10),
a1 = f(n),
a2 = s(n),
d = Math.abs(a1 - a2);

p(f(n));
p(s(n));
p(`誤差: \${d}`)
};

input0.onchange = output;
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();
```

n =