2017年12月25日月曜日

学習環境

代数系入門 (松坂 和夫(著)、岩波書店)の第1章(整数)、6(同値関係、合同式)、問題1.を取り組んでみる。


  1. a - b + m k = a - b - m k = ml - m k = m l - k

    よって、

    a b m o d m a b + m k m o d m

コード(Emacs)

Python 3

#!/usr/bin/env python3


def mod(a, b, m):
    return (a - b) % m == 0

a = 11
b = 21
m = 2


for k in range(10):
    print(f'{k}: {mod(a, b + k * m, m)}')

入出力結果(Terminal, Jupyter(IPython))

$ ./sample1.py
0: True
1: True
2: True
3: True
4: True
5: True
6: True
7: True
8: True
9: True
$

HTML5

<pre id="output0"></pre>

<input id="a0" type="number" step="1" value="3"><input id="b0" type="number" step="1" value="5">
+
<input id="k0" type="number" step="1" value="10"><input id="m0" type="number" step="1" value="2">
(mod <span id="m1"></span>)
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample1.js"></script>

JavaScript

let pre0 = document.querySelector('#output0'),
    run0 = document.querySelector('#run0'),
    clear0 = document.querySelector('#clear0'),
    input_a0 = document.querySelector('#a0'),
    input_b0 = document.querySelector('#b0'),
    input_k0 = document.querySelector('#k0'),
    input_m0 = document.querySelector('#m0'),
    span_m1 = document.querySelector('#m1'),
    inputs = [input_a0, input_b0, input_k0, input_m0],
    range = (n) => {
        let result = [];

        for (let i = 0; i < n; i += 1) {
            result.push(i);
        }
        
        return result;
    },
    p = (text) => pre0.textContent += text + '\n',
    clear = () => pre0.textContent = '',
    output = () => {
        let a0 = parseInt(input_a0.value, 10),
            b0 = parseInt(input_b0.value, 10),
            k0 = parseInt(input_k0.value, 10),
            m0 = parseInt(input_m0.value, 10);

        span_m1.textContent = m0;

        p((a0 - (b0 + k0 * m0)) % m0 == 0);
    };

run0.onclick = output;
clear0.onclick = clear;
inputs.forEach((input) => input.onchange = output);
output();



+

(mod )

0 コメント:

コメントを投稿