## 2017年12月28日木曜日

### 数学 - Python - JavaScript - 代数学 - 整数 - 同値関係、合同式(法、公約数)

1. $\begin{array}{}a-b=mk\\ {a}_{1}d-{b}_{1}d={m}_{1}dk\\ {a}_{1}-{b}_{1}={m}_{1}k\end{array}$

よって、

${a}_{1}\equiv {b}_{1}\left(mod\left({m}_{1}\right)\right)$

コード(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, 10)
b += ((a % m) - (b % m))
d = gcd([a, b, m])
for t in [a, b, mod(a, b, m), mod(a / d, b / d, m / d)]:
pprint(t)
print()


$./sample4.py 11 -85 True True -17 -65 True True 95 -27 True True 37 91 True True 30 55 True True 28 -62 True True 73 39 True True 18 90 True True 70 -62 True True 79 22 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="sample4.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 % d == 0 && b % d == 0 && m % d == 0) {
p(mod(a, b, m));
p(mod(a / d, b / d, m / d));
} else {
p(${d}は${a}、${b}、${m}の公約数ではない);
}
};

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


(mod )

d =