センター数学1A（2018年）をpythonで解いてみた

- 実行環境
 - VirtualBox 5.2.2
 - Cent6.9
 - python3.5

問題は参考書または、インターネットを参照ください。

# 第1問
## (1)

プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
import math

x = symbols('x')
n = symbols('n')

f = (x + n) * (n + 5 - x)
print(expand(f))
print(expand(f.subs([(n, 1)])))
print(expand(f.subs([(n, 2)])))

h = x * (5 - x)
a = h.subs([(x, (5 + math.sqrt(17))/2)])
print(a)

g = x * (x + 1) * (x + 2) * (5 - x) * (6 - x) * (7 - x)
b = int(round((g.subs([(x, (5 + math.sqrt(17))/2)])), 1))
print(factorint(b))
```

出力

```
n**2 + 5*n - x**2 + 5*x
-x**2 + 5*x + 6
-x**2 + 5*x + 14
2.00000000000000
{2: 8}
```

解答
ア:5、イ:6、ウ:1、エ:4、オ:2、カ:8

##(2)
実数の比較は検討中
プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
from sympy.solvers.inequalities import *
import math

x = Symbol('x', real=True)

a = set([])
notA = set([])
b = set([])
c = set([])
for num in range(1, 21) :
    if 20 % num == 0 :
        a.add(num)
    else :
        notA.add(num)
    if num % 3 == 0 :
        b.add(num)
    if num % 2 == 0 :
        c.add(num)

print("A < C=", a<=c)
print("A & B=", a&b)

print("(A | C) & B=", (a | c) & b)
print("(~A & C) | B=" ,(notA & c) | b)
print("~A & (B | C)=", notA & (b | c))

print("p:", reduce_abs_inequality(Abs(x - 2) - 2, '<', x))
print("s:", reduce_rational_inequalities([[x**2 < 16]], x))
print(reduce_rational_inequalities([[x**2 < 16]], x))
```

出力

```
A < C= False
A & B= set()
(A | C) & B= {18, 12, 6}
(~A & C) | B= {3, 6, 8, 9, 12, 14, 15, 16, 18}
~A & (B | C)= {3, 6, 8, 9, 12, 14, 15, 16, 18}
p: (0 < x) & (x < 4)
s: (-4 < x) & (x < 4)
```

解答
キ:2、ク:0、ケ:2、コ:0

## (3)

プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
import math
from scipy.optimize import root
from scipy.misc import derivative
from sympy.solvers.inequalities import *
from sympy.solvers.inequalities import solve_univariate_inequality

a = symbols('a')
x = symbols('x')

y = a*x**2-2*(a+3)*x-3*a+21

domain = Interval(0, S.Infinity)
p=solve(diff(y, x), x)
print(p[0].expand())
print(solve_univariate_inequality(4 <= p[0], a, False, domain))
print(solve_univariate_inequality(p[0] <= 4, a, False, domain))

print(solve(y.subs(x, 4) - 1, a))

z = ((y.subs(x, p[0]) - 1) * a).expand()

print(solve(z, a)[1])
```

出力

```
1 + 3/a
Interval.Lopen(0, 1)
Interval(1, oo)
[4/5]
sqrt(13)/4 + 7/4
```

解答
サ:1、シ:3、ス:1、セ:1、ソ:4、タ:5、チ:7、ツ:1、テ:3、ト:4

# 第2問
## (1)

プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
import math
from scipy.optimize import root
from scipy.misc import derivative
from sympy.solvers.inequalities import *
from sympy.solvers.inequalities import solve_univariate_inequality

x = symbols('x')
sinx = symbols('sinx')
a = symbols('a')
b = symbols('b')
c = symbols('c')
bd = symbols('bd')

ab = 5
bc = 9
cd = 3
ac = 6
g = 0

cos = (b**2+c**2-a**2)/(2*b*c)
cosSubs = cos.subs(a, ac).subs(b, ab).subs(c, bc)
print(cosSubs)
f = sinx**2 + cosSubs**2 - 1
sinSubs = solve(f, sinx)[1]
print(sinSubs)
g = a**2-b**2-c**2+2*b*c*(-1 * cosSubs)
if ab * sinSubs > cd :
    print("CD < AB*sinABC")
    print("AB//CD")
    print(solve(g.subs(b, bc).subs(c, cd), a)[1])
else :
    print("CD > AB*sinABC")
    print("AD//BD")
    h = 9 - a**2-b**2-c**2+2*b*c*cosSubs
    cd = solve(h.subs(a, 3).subs(b, 5), c)[1]
    print(sovle(g.subs(b, ab).subs(c, cd), a)[1])
```

出力

```
7/9
4*sqrt(2)/9
CD < AB*sinABC
AB//CD
2*sqrt(33)
```

解答
ア:7、イ:9、ウ:4、エ:2、オ:9、カ:0、キ:4、ク:2、ケ:3、コ:3

## (2)
### (1)

プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
import math
from scipy.optimize import root
from scipy.misc import derivative
from sympy.solvers.inequalities import *
from sympy.solvers.inequalities import solve_univariate_inequality

# 男子短距離
shortBoy = {
  'min' : 152,
  'oneQuarter' : 176,
  'middle' : 181,
  'threeQuarter' : 186,
  'max' : 202,
  'lowerLimit' : 180,
  'upperLimit' : 185
}

# 男子長距離
longBoy = {
  'min' : 155,
  'oneQuarter' : 172,
  'middle' : 176,
  'threeQuarter' : 181,
  'max' : 198,
  'lowerLimit' : 170,
  'upperLimit' : 175
}

# 女子短距離
shortGirl = {
  'min' : 145,
  'oneQuarter' : 165,
  'middle' : 170,
  'threeQuarter' : 174,
  'max' : 187,
  'lowerLimit' : 165,
  'upperLimit' : 170
}

# 女子長距離
longGirl = {
  'min' : 151,
  'oneQuarter' : 161,
  'middle' : 165,
  'threeQuarter' : 170,
  'max' : 186,
  'lowerLimit' : 165,
  'upperLimit' : 170
}

itemList = [shortBoy, longBoy, shortGirl, longGirl]
cnt=0
tmpRange=0

for item in itemList :
    range = item['max'] - item['min']
    if range > tmpRange :
        tmpRange = range
        cnt+=1

if cnt == 3 :
    print('0 is trure')

flag = false
for item in itemList :
    range = item['threeQuarter'] - item['oneQuarter']
    if range >= 12 :
        flag = true
        break
    flag = true

if flag :
    print('1 is ture')

if longBoy['middle'] <= longBoy['upperLimit'] and longBoy['middle'] >= longBoy['lowerLimit'] :
    print('2 is true')

if longGirl['oneQuarter'] >= longGirl['lowerLimit'] and longGirl['oneQuarter'] <= longGirl['upperLimit'] :
    print('3 is true')

cnt = 0
tmpMax = 0
for item in itemList :
    if item['max'] > tmpMax :
        tmpMax = item['max']
        cnt+=1

if cnt == 2 :
    print('4 is trure')

cnt = 1
tmpMin = shortBoy['min']
for item in itemList :
    if item['min'] < tmpMin :
        tmpMin = item['min']
        cnt+=1

if cnt == 4 :
    print('5 is trure')

if (shortBoy['middle'] >= 180 and shortBoy['middle'] <= 182) and (longBoy['threeQuarter'] >= 180 and longBoy['threeQuarter'] <= 182) :
    print('6 is true')
```

出力

```
1 is ture
6 is true
```

解答
サ:1、シ:6（順不同）

### (2)

プログラム
検討中

## (3)
帰納的にnは自然数であれば問題ないので、n=3で試行します。

プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
import math
from scipy.optimize import root
from scipy.misc import derivative
from sympy.solvers.inequalities import *
from sympy.solvers.inequalities import solve_univariate_inequality

x = symbols('x')
x1 = symbols('x1')
x2 = symbols('x2')
x3 = symbols('x3')
w = symbols('w')
w1 = symbols('w1')
w2 = symbols('w2')
w3 = symbols('w3')

f = (x1-x)*(w1-w)+(x2-x)*(w2-w)+(x3-x)*(w3-w)
print(f.expand().subs(x1*w+x2*w+x3*w, 3*x*w).subs(w1*x+w2*x+w3*x, 3*x*w))
[ryo@CentOS 1A]$ python3 2-3.py
-3*w*x + w1*x1 + w2*x2 + w3*x3
```

出力

```
-3*w*x + w1*x1 + w2*x2 + w3*x3
```

解答
ソ:2

#第3問

プログラム

```python
# -*- coding: utf-8 -*-

import sympy as sym
import math

bigDice = (1, 2, 3, 4, 5, 6)
smallDice = (1, 2, 3, 4, 5, 6)

p_a = sym.Rational(bigDice.count(4),bigDice.__len__())
print("P(A):", p_a)

cnt = 0
a_b_cnt = 0
nota_b_cnt = 0
for bigNum in bigDice:
    for smallNum in smallDice:
        if bigNum + smallNum == 7:
            cnt+=1
            if bigNum == 4:
                a_b_cnt+=1
            else:
                nota_b_cnt+=1

p_b = sym.Rational(cnt,bigDice.__len__()*smallDice.__len__())
print("P(B):", p_b)
p_a_b = sym.Rational(a_b_cnt,bigDice.__len__()*smallDice.__len__())

cnt = 0
a_c_cnt = 0
nota_c_cnt = 0
for bigNum in bigDice:
    for smallNum in smallDice:
        if bigNum + smallNum == 9:
            cnt+=1
            if bigNum == 4:
                a_c_cnt+=1
            else:
                nota_c_cnt+=1

p_c = sym.Rational(cnt,bigDice.__len__()*smallDice.__len__())
print("P(C):", p_c)

p_a_c = sym.Rational(a_c_cnt,bigDice.__len__()*smallDice.__len__())
p_ac = p_a_c / p_c
print("P(A|C):", p_ac)

p_ca = p_a_c / p_a
print("P(C|A):", p_ca)

if p_a_b == p_a * p_b :
    print("P(A and B) = P(A)P(B)")
elif p_a_b < p_a * p_b :
    print("P(A and B) < P(A)P(B)")
elif p_a_b > p_a * p_b :
    print("P(A and B) > P(A)P(B)")

if p_a_c ==  p_a * p_c :
    print("P(A and C) = P(A)P(C)")
elif p_a_c < p_a * p_c :
    print("P(A and C) < P(A)P(C)")
elif p_a_c > p_a * p_c :
    print("P(A and C) > P(A)P(C)")

p_nota_c = sym.Rational(nota_c_cnt,bigDice.__len__()*smallDice.__len__())
print(p_a_b * p_nota_c)

p_nota_b = sym.Rational(nota_b_cnt,bigDice.__len__()*smallDice.__len__())
print((p_a_b * p_nota_c + p_nota_b * p_a_c) * 2)
```

出力

```
P(A): 1/6
P(B): 1/6
P(C): 1/9
P(A|C): 1/4
P(C|A): 1/6
P(A and B) = P(A)P(B)
P(A and C) > P(A)P(C)
1/432
1/81
```

解答
ア:1、イ:6、ウ:1、エ:6、オ:1、カ:9、キ:1、ク:4、ケ:1、コ:6
サ:1、シ:2、ス:1、セ:4、ソ:3、タ:2、チ:1、ツ:8、テ:1


#第4問
ケ、コサシは144と7が互いに素なので明らかとしています。

プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
import math
from scipy.optimize import root
from scipy.misc import derivative
from sympy.solvers.inequalities import *
from sympy.solvers.inequalities import solve_univariate_inequality

def sumFact(numList):
    fact_sum = 1
    for num in numList:
        fact_sum*=(numList[num]+1)
    return fact_sum

x = symbols('x')
y = symbols('y')
k = symbols('k')

fact_num = factorint(144)
print(fact_num)
print(sumFact(fact_num))

f = 144*x-7*y-1
num = 1
while true:
    ans = solve(f.subs(x,num),y)[0]
    if float(ans).is_integer():
        break
    num+=1

print("x=", num)
print("y=", ans)

rc_x = 7*k+num
rc_y = 144*k+ans

cnt = 0
flag_a = 0
flag_b = 0
while true:
    ans = (7*(rc_y)+1).subs(k, cnt)
    target_num = factorint(ans)
    if sumFact(target_num) == 18 and flag_a == 0:
        print("ス:", ans/144)
        flag_a = 1
    if sumFact(target_num) == 30 and flag_b == 0:
        print("セソ:", ans/144)
        flag_b = 1

    if flag_a == 1 and flag_b == 1:
        break
    cnt+=1
```

出力

```
{2: 4, 3: 2}
15
x= 2
y= 41
ス: 2
セソ: 23
```

解答
ア:4、イ:3、ウ:2、エ:1、オ:5、カ:2、キ:4、ク:1、ケ:7、コ:1
サ:4、シ:4、ス:2、セ:2、ソ:3

#第5問

プログラム

```python
# -*- coding: utf-8 -*-

from sympy import *
import math
from scipy.optimize import root
from scipy.misc import derivative
from sympy.solvers.inequalities import *
from sympy.solvers.inequalities import solve_univariate_inequality

x = symbols('x')
sinx = symbols('sinx')
a = symbols('a')
b = symbols('b')
c = symbols('c')
cf = symbols('bd')
fa = symbols('fa')
bf = symbols('bf')

ab = 2
ac = 1
angleA=90

# 余弦定理
f = (b**2+c**2-a**2)/(2*b*c) - int(math.cos(math.radians(angleA)))
bc = (solve(f.subs(c, ac).subs(b, ab).subs(c, ac).subs(x, angleA), a)[1])
bd = bc * 2/3
print("BD=", bd)
# 方べきの定理
print("AB*BE=", bd**2)
be = bd**2/ab
print("BE=", be)

if be/bd < ab/bc :
    print("<")
    print("C")
elif be/bd > ab/bc :
    print(">")
    print("A")
elif be/bd == ab/bc :
    print("=")
    print("not cross")

cd = bc * 1/3
ae = 2- be
# メネラウスの定理
g = bd/cd * cf/fa * ae/be - 1
print(solve(g, cf/fa))
cf_af = solve(g, cf/fa)[0]
cf = (cf_af*ac)/(1-cf_af)
print(cf)
af = ac + cf
bf = solve(x**2-ab**2-af**2, x)[1]

if cd/bd == 2 :
     print("重心")
elif bd/bc == be/ab :
     print("外心")
elif bf/af == be/ae :
     print("内心")
```

出力

```
BD= 2*sqrt(5)/3
AB*BE= 20/9
BE= 10/9
<
C
[5/8]
5/3
内心
```

解答
ア:2、イ:5、ウ:3、エ:2、オ:0、カ:9、キ:1、ク:0、ケ:9、コ:0
サ:4、シ:5、ス:8、セ:5、ソ:3、タ:1