2017年12月29日金曜日

学習環境

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


  1. d を m の任意の約数とすれば、ある整数 k が存在して、

    m = d k k

    とおくことができる。

    よって、

    a - b = d k s s a - b = k s d

    ゆえに、

    a b m o d d

コード(Emacs)

Python 3

#!/usr/bin/env python3
from sympy import pprint, gcd
import random


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

for _ in range(10):
    a = random.randrange(-100, 100)
    b = random.randrange(-100, 100)
    m = random.randrange(1, 20)
    b += ((a % m) - (b % m))
    for d in range(1, m + 1):
        if m % d == 0:
            break
    print(f'a = {a}, b = {b}, m = {m}, d = {d}')
    for t in [mod(a, b, m), mod(a, b, d)]:
        pprint(t)
    print()

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

$ ./sample5.py
a = -98, b = -93, m = 5, d = 1
True
True

a = -14, b = 25, m = 13, d = 1
True
True

a = 13, b = -55, m = 4, d = 1
True
True

a = 13, b = -52, m = 1, d = 1
True
True

a = -12, b = 20, m = 2, d = 1
True
True

a = 79, b = 24, m = 5, d = 1
True
True

a = 79, b = -41, m = 10, d = 1
True
True

a = 98, b = -72, m = 10, d = 1
True
True

a = 8, b = 62, m = 9, d = 1
True
True

a = -80, b = 80, m = 10, d = 1
True
True

$

HTML5

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

<input id="a0" type="number" step="1" value="12"><input id="b0" type="number" step="1" value="22">
(mod <input id="m0" type="number" step="1" value="10">)
<br>
d = <input id="d0" type="number" min="1" step="1" value="2">
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample5.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_m0 = document.querySelector('#m0'),
    input_d0 = document.querySelector('#d0'),
    inputs = [input_a0, input_b0, input_m0, input_d0],
    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 = '',
    mod = (a, b, m) => (a - b) % m === 0,
    output = () => {
        let a = parseInt(input_a0.value, 10),
            b = parseInt(input_b0.value, 10),
            m = parseInt(input_m0.value, 10),
            d = parseInt(input_d0.value, 10);

        if ((a - b) % m == 0) {
            if (m % d == 0) {
                p(mod(a, b, m));
                p(mod(a, b, d));
            } else {
                p(`${d}は${m}の約数ではない`);
            }
        } else {
            p(`${a}と${b}は${m}を法として合同ではない`);
        }
    };

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



(mod )

d =

0 コメント:

コメントを投稿