## 2017年12月27日水曜日

### 数学 - Python - JavaScript - 代数学 - 整数 - 同値関係、合同式(法、正の整数倍)

1. $\begin{array}{}ak-bk\\ =\left(a-b\right)k\\ =mnk\left(n\in \text{ℤ}\right)\\ =n\left(mk\right)\end{array}$

よって、

$ak\equiv bk\left(mod\left(mk\right)\right)$

コード(Emacs)

Python 3

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

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

k = symbols('k', integer=True, positive=True)

for _ in range(10):
a = random.randrange(-100, 100)
b = random.randrange(-100, 100)
m = random.randrange(1, 11)
for t in [mod(a, b, m), mod(a, b, m)]:
if t:
print(f'{a} ≡ {b} (mod {m}): {t}')
print(f'{a * k} ≡ {b * k} (mod {m * k}): {mod(a * k, b * k, m * k)}')
print()


$./sample3.py 10 ≡ -47 (mod 1): True 10*k ≡ -47*k (mod k): True 10 ≡ -47 (mod 1): True 10*k ≡ -47*k (mod k): True 36 ≡ 38 (mod 1): True 36*k ≡ 38*k (mod k): True 36 ≡ 38 (mod 1): True 36*k ≡ 38*k (mod k): True -2 ≡ -50 (mod 6): True -2*k ≡ -50*k (mod 6*k): True -2 ≡ -50 (mod 6): True -2*k ≡ -50*k (mod 6*k): True$


HTML5

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

<input id="a0" type="number" step="1" value="6">
≡
<input id="b0" type="number" step="1" value="11">
(mod <input id="m0" type="number" step="1" value="5">)
<br>
k = <input id="k0" type="number" min="1" step="1" value="2">
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample3.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_k0 = document.querySelector('#k0'),
inputs = [input_a0, input_b0, input_m0, input_k0],
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),
k = parseInt(input_k0.value, 10);

p(mod(a, b, m));
p(mod(a * k, b * k, m * k));
};

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


(mod )

k =