学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第3章(因数分解と分数式)、2(整式の最大公約数と最小公倍数)の問10の解答を求めてみる。
最大公約数、最小公倍数はそれぞれ、
(数係数は省略。以下同様。)
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, gcd, lcm
print('9.')
class MyTest(TestCase):
def test1(self):
x, y, z, a, b, c = symbols('x, y, z, a, b, c', real=True)
spam = [(x * y ** 2 * z ** 3, x ** 3 * y ** 2 * z),
(x ** 2 * y ** 3, x * y ** 2, x ** 4 * y),
(a ** 2 * b ** 3, a ** 3 * b * c ** 2, a ** 2 * b ** 2 * c),
(a ** 3 * (x - 1), a ** 2 * (x - 1) ** 3),
(x * (x + 1) * (x + 2), (x + 1) * (x + 2) * (x + 3)),
(x ** 3 * (x + 2) ** 2 * (x + 1), x ** 2 * (x + 3)
* (x + 2), x ** 2 * (x + 1) * (x + 3) ** 2),
(x ** 2 - 3 * x + 2, x ** 2 - 9 * x + 14),
(x ** 2 - 9, x ** 2 - 2 * x - 3, x ** 2 + 4 * x + 3),
(x ** 2 + x - 6, x ** 2 - 6 * x + 8, x ** 2 - x - 12),
(a ** 2 + 2 * a * b + b ** 2, a ** 2 - b ** 2, a ** 3 + b ** 3),
(x ** 2 + 5 * x + 6, x ** 2 + 6 * x + 9, x ** 3 + 8),
(x ** 2 + 2 * x - 3, x ** 3 + 3 * x ** 2 - x - 3, x ** 3 + 2 * x ** 2 - x - 2)]
gcds = [x * y ** 2 * z,
x * y,
a ** 2 * b,
a ** 2 * (x - 1),
(x + 1) * (x + 2),
x ** 2,
x - 2,
1,
1,
a + b,
1,
x - 1]
lcms = [x ** 3 * y ** 2 * z ** 3,
x ** 4 * y ** 3,
a ** 3 * b ** 3 * c ** 2,
a ** 3 * (x - 1) ** 3,
x * (x + 1) * (x + 2) * (x + 3),
x ** 3 * (x + 1) * (x + 2) ** 2 * (x + 3) ** 2,
(x - 1) * (x - 2) * (x - 7),
(x - 3) * (x + 1) * (x + 3),
(x - 4) * (x - 2) * (x + 3),
(a - b) * (a + b) ** 2 * (a ** 2 - a * b + b ** 2),
(x + 2) * (x + 3) ** 2 * (x ** 2 - 2 * x + 4),
(x - 1) * (x + 1) * (x + 2) * (x + 3)]
for s, g, l in zip(spam, gcds, lcms):
self.assertEqual(gcd(s).factor(), g)
self.assertEqual(lcm(s).factor(), l)
if __name__ == '__main__':
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample10.py -v
9.
test1 (__main__.MyTest) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.204s
OK
%
0 コメント:
コメントを投稿