2016年9月17日土曜日

学習環境/開発環境

代数系入門 (松坂 和夫(著)、岩波書店)の第1章(整数)、2(数学的帰納法と除法の定理)、問題4.を取り組んでみる。

問題4.

r=1 ( n 0 )+( n 1 )=1+ n! 1!( n1 )! =n ( n+1 1 )= ( n+1 )! 1!( n+11 )! =n r=k ( n k1 )+( n k )=( n+1 k ) r=k+1 ( n ( k+1 )1 )+( n k+1 ) = n! k!( nk )! + n! ( k+1 )!( n( k+1 ) )! = ( k+1 )n!+( nk )n! ( k+1 )!( nk )! = ( n+1 )! ( k+1 )!( nk )! = n+1k k+1 · ( n+1 )! k!( ( n+1 )k )! = n+1k k+1 ·( n+1 k ) = n+1k k+1 ·( ( n k1 )+( n k ) ) = n+1k k+1 ·( n! ( k1 )!( nk+1 )! + n! k!( nk )! ) = n+1k k+1 · kn!+( nk+1 )n! k!( nk+1 )! = ( n+1 )! ( k+1 )!( nk )! = ( n+1 )! ( k+1 )!( ( n+1 )( k+1 ) )! =( n+1 k+1 )

JavaScript で確認。

HTML5

<label for="n0">n = </label>
<input id="n0" type="number" min="1" step="1" value="1">
<label for="r0">r = </label>
<input id="r0" type="number" min="1" step="1" value="1">
<div id="output0"></div>

<script src="array.js"></script>
<script src="sample4.js"></script>

JavaScript

コード(Emacs)

var input_n = document.querySelector('#n0'),
    input_r = document.querySelector('#r0'),
    inputs = [input_n, input_r],
    div_output = document.querySelector('#output0'),
    nl = '<br>',
    factorial,
    combination,
    output;

console.log(inputs);
factorial = function (n) {
    return Array.range(1, n + 1)
        .reduce(function (x, y) {
            return x * y;
        }, 1);
};
combination = function (n, r) {
    return factorial(n) / (factorial(r) * factorial(n - r));
};

output = function () {
    var left,
        right,
        n = parseInt(input_n.value, 10),
        r = parseInt(input_r.value, 10);

    if (1 <= r && r <= n) {
        left = combination(n, r - 1) + combination(n, r);
        right = combination(n + 1, r);
        div_output.innerHTML =
            '左辺 = ' + left + nl +
            '右辺 = ' + right + nl +
            '左辺 = 右辺 ' + (left === right) + nl +
            '|左辺 - 右辺| = ' + Math.abs(left - right) + nl;
    }
};

inputs.forEach(function (input) {
    input.onchange = output;
});

output();

0 コメント:

コメントを投稿

Comments on Google+: