## 2019年12月14日土曜日

### 数学 - Python - 代数学 - 整式の計算 - 割り算、商と剰余、次数

1. $\begin{array}{l}6{x}^{3}-5{x}^{2}-4\\ =\left(3{x}^{2}+x-2\right)\left(2x-\frac{7}{3}\right)+\frac{19}{3}x-\frac{26}{3}\end{array}$

2. よって商、 余りはそれぞれ

$\begin{array}{l}{x}^{2}+2x+4\\ 33x+20\end{array}$

3. よって商は

$2{a}^{3}+{a}^{2}+2$

余りは零。

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Rational
import random

print('8.')

x = symbols('x')

class MyTest(TestCase):
def test1(self):
nums = [6 * x ** 3 - 5 * x ** 2 - 4,
x ** 4 - 3 * x ** 3 - 10 * x ** 2 + 5 * x + 4,
4 * x ** 5 - 9 * x ** 3 - 2 * x - 8]
dens = [3 * x ** 2 + x - 2,
x ** 2 - 5 * x - 4,
2 * x ** 2 - x - 4]
qs = [2 * x - Rational(7, 3),
x ** 2 + 2 * x + 4,
2 * x ** 3 + x ** 2 + 2]
rs = [19 * x / 3 - Rational(26, 3),
33 * x + 20,
0]

for n, d, q, r in zip(nums, dens, qs, rs):
for _ in range(10):
x0 = {x: random.randrange(-100, 101)}
self.assertEqual(n.subs(x0), (d * q + r).subs(x0))

if __name__ == '__main__':
main()


% ./sample8.py -v
8.
test1 (__main__.MyTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.071s

OK
%