## 2020年7月3日金曜日

### 数学 - Python - 代数学 - 不等式 - 2次不等式 - 2つの不等式を同時に成り立たせる整数値、平方根

1. $\begin{array}{l}\left(x-5\right)\left(x+2\right)\ge 0\\ x\le -2,5\le x\end{array}$
$\begin{array}{l}{x}^{2}-2x-40=0\\ x=1±\sqrt{1+40}=1±\sqrt{41}\\ 1-\sqrt{41}
$1-\sqrt{41}
$6<\sqrt{41}<7$

よって、 問題の2つの 不等式を満たす整数値は

$-5,-4,-3,-2,5,6,7$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import pprint, S, symbols, plot
from sympy.solvers.inequalities import reduce_inequalities

print('9.')

x = symbols('x', integer=True)
f = x ** 2 - 3 * x - 10
g = x ** 2 - 2 * x - 40

class Test(TestCase):
def test(self):
blns = [False] + [True] * 4 + [False] * 6 + [True] * 3 + [False]
for n, bln in zip(list(range(-6, 8)), blns):
print(f'x = {n}')
d = {x: n}
self.assertEqual(f.subs(d) >= 0 and g.subs(d) < 0, bln)

pprint(reduce_inequalities([f >= 0, g < 0], x))

p = plot(f, g,
(x, -10, 10),
legend=True,
show=False)

colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
o.line_color = color
p.show()
p.save('sample9.png')

if __name__ == "__main__":
main()


 ./sample9.py -v
9.
(5 ≤ x ∧ x < 1 + √41) ∨ (x ≤ -2 ∧ 1 - √41 < x)
test (__main__.Test) ... x = -6
x = -5
x = -4
x = -3
x = -2
x = -1
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
ok

----------------------------------------------------------------------
Ran 1 test in 0.010s

OK
%