LoginSignup
5
5

More than 5 years have passed since last update.

センター数学1A(2019年)をpythonで解いてみた

Posted at
  • 実行環境
    • VirtualBox 5.2.2
    • Cent6.9
    • python3.5.4

第1問

(1)

根号の中の式を取得するのに無理をしました。
場合分けはその範囲内のaを代入して確かめれば十分。

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np

def getA(num) :
    if (a_coeff*a-1).subs(a, num) > 0 :
        if (a+2).subs(a, num) > 0 :
            return a_coeff*a-1 + (a+2)
        else :
            return a_coeff*a-1 + (-1) * (a+2)

    else :
        if (a+2).subs(a, num) > 0 :
            return -1*(a_coeff*a-1) + (a+2)
        else :
            return -1*(a_coeff*a-1) + (-1) * (a+2)


a = symbols('a')
f = 9*a**2-6*a+1
print(f.factor())
a_coeff =  np.reciprocal(solve(f, a)[0])

A = getA(1)
print(A)
if solve(A-(2*a+13), a)[0] > 1/3 :
    print("a =", solve(A-(2*a+13), a)[0])

A = getA(-1)
print(A)
if solve(A-(2*a+13), a)[0] >= -2 and solve(A-(2*a+13), a)[0] <= 1/3 :
    print("a =", solve(A-(2*a+13), a)[0])

A = getA(-3)
print(A)
if solve(A-(2*a+13), a)[0] < -2 :
    print("a =", solve(A-(2*a+13), a)[0])

出力

(3*a - 1)**2
4*a + 1
a = 6
-2*a + 3
-4*a - 1
a = -7/3

解答
ア:3、イ:1、ウ:4、エ:1、オ:-
カ:2、キ:3、ク:6、ケ:-、コ:7
サ:3

(2)

命題の解き方を検討中。

(3)

微分が楽ですが、学生時代は平方完成で頑張っていました。
プログラムで平方完成を実現させたいです。

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np

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

f = x**2+(2*a-b)*x+a**2+1
vertex_x = solve(diff(f, x), x)
vertex_y = f.subs(x, vertex_x[0]).expand()
print("頂点のx座標 :", vertex_x)
print("頂点のy座標 :", vertex_y)
g = solve(f.subs(x, -1)-6, b)[0]
vertex_a = solve(diff(g, a), a)
vertex_b = g.subs(a, vertex_a[0])
print("bの最大値 :", vertex_b)
print("そのときのa :", vertex_a)
ff = f.subs(a, vertex_a[0]).subs(b, vertex_b)
vertex_ff_x = solve(diff(ff, x), x)
vertex_ff_y = ff.subs(x, vertex_ff_x[0])
print("ニヌ :", vertex_ff_x)
print("ネノハ :", vertex_ff_y)

出力

頂点のx座標 : [-a + b/2]
頂点のy座標 : a*b - b**2/4 + 1
bの最大値 : 5
そのときのa : [1]
ニヌ : [3/2]
ネノハ : -1/4

解答
チ:2、ツ:4、テ:1、ト:5、ナ:1
ニ:3、ヌ:2、ネ:-、ノ:1、ハ:4

第2問

(1)

プログラム

sqrtが計算されてしますので、分数と根号を分けて出力するようにしました。

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np

x = symbols('x')

ab = 3
bc = 4
ac = 2

cosBAC = (ab**2+ac**2-bc**2)/(2*ab*ac)
print("cosBAC =", cosBAC)

if cosBAC > 0 :
    print("BACは鋭角")
elif cosBAC == 0 :
    print("BACは直角")
else :
    print("BACは鈍角")

reciprocalCosBac = np.reciprocal(cosBAC)
print("オカ :", reciprocalCosBac**2-1)
print("キ :", abs(reciprocalCosBac))
sinBAC = sqrt(1-cosBAC**2)

cosCAD = -1*cosBAC
print("cosCAD =", cosCAD)
ad = np.reciprocal(cosCAD)
print("AD =", ad)
reciprocalCosCad = np.reciprocal(cosCAD)
sinCAD = sqrt(1-cosCAD**2)
ABC = 1/2*ab*ac*1/abs(reciprocalCosBac)
ADC = 1/2*ac*ad*1/abs(reciprocalCosCad)
DBC = ABC + ADC
print("サセ :", DBC)
print("シス :", reciprocalCosBac**2-1)

出力

cosBAC = -0.25
BACは鈍角
オカ : 15.0
キ : 4.0
cosCAD = 0.25
AD = 4.0
サセ : 1.75
シス : 15.0

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

(2)

(3)

相変わらず統計が苦手です。
学生時代にやっていなかったこともあり、解法検討中です。
ヒストグラムから箱ひげ図を、またはその逆の作成方法を考えています。

第3問

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np

redBag = ('red', 'red', 'white')
whiteBag = ('red', 'white')

dice = (1, 2, 3, 4, 5, 6)

cnt = 0
for num in dice :
    if num % 3 == 0 :
        cnt += 1

p_dice_three_times = Rational(cnt, dice.__len__())
p_dice_not_three_times = 1-p_dice_three_times
p_redBag_red = Rational(redBag.count('red'), redBag.__len__())
p_redBag_white = Rational(redBag.count('white'), redBag.__len__())
p_whiteBag_red = Rational(whiteBag.count('red'), whiteBag.__len__())
p_whiteBag_white = Rational(whiteBag.count('white'), whiteBag.__len__())
p_red_red = p_dice_not_three_times * p_redBag_red
p_white_red = p_dice_three_times * p_whiteBag_red
print("アイ :", p_red_red)
print("ウエ :", p_white_red)

p_second_whiteBag = p_dice_not_three_times * p_redBag_white + p_dice_three_times * p_whiteBag_white
print("オカキ :", p_second_whiteBag)

p = symbols('p')

p_second_white = p * p_whiteBag_white + (1-p) * p_redBag_white
print("クケ :", p_second_white)
p_first_white = p_dice_three_times * p_whiteBag_white + p_dice_not_three_times * p_redBag_white
print("コサシスセ :", p_second_white.subs(p, p_first_white))

p_third_white = p_second_white * p_whiteBag_white + (1-p_second_white) * p_redBag_white
print("ソタチツテト :", p_third_white.subs(p, p_first_white))

print("ナニヌネ :", ((p * p_whiteBag_white)/p_second_white).subs(p, p_first_white))
print("ノハヒフヘ :", ((1-p)*p_redBag_red*p_redBag_white/p_third_white).subs(p, p_first_white))

出力

アイ : 4/9
ウエ : 1/6
オカキ : 7/18
クケ : p/6 + 1/3
コサシスセ : 43/108
ソタチツテト : 259/648
ナニヌネ : 21/43
ノハヒフヘ : 88/259

解答
ア:4、イ:9、ウ:1、エ:6、オ:7
カ:1、キ:8、ク:1、ケ:6、コ:4
サ:3、シ:1、ス:0、セ:8、ソ:2
タ:5、チ:9、ツ:6、テ:4、ト:8
ナ:2、ニ:1、ヌ:4、ネ:3、ノ:8
ハ:8、ヒ:2、フ:5、ヘ:9

第4問

49と23は互いに素なのでエオカキは明らか。
連続する3つの数字の1番目と3番目の公約数は1または2。
(1番目が奇数なら1、偶数なら1と2といった具合。)

プログラム

# -*- coding: utf-8 -*-

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

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

f = 49*x-23*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 = 23*k+num
rc_y = 49*k+ans

g = 49*x-23*y+1
tmpNum = 1
while true :
    tmpAns = solve(g.subs(x,tmpNum),y)[0]
    if float(tmpAns).is_integer():
        break
    tmpNum += 1

if tmpNum < num :
    print("ク :", tmpNum)
    print("ケコ :", tmpAns)
    minNum = tmpNum
    minAns = tmpAns
else :
    print("ク :", num)
    print("ケコ :", ans)
    minNum = num
    minAns = ans

h = 49*x-23*y-2
num = 1
while true :
    ans = solve(h.subs(x,num),y)[0]
    if float(ans).is_integer():
        break
    num += 1

i = 49*x-23*y+2
tmpNum = 1
while true :
    tmpAns = solve(i.subs(x,tmpNum),y)[0]
    if float(tmpAns).is_integer():
        break
    tmpNum += 1

if tmpNum < num :
    print("サ :", tmpNum)
    print("シス :", tmpAns)
    num = tmpNum
    ans = tmpAns
else :
    print("サ :", num)
    print("シス :", ans)

a = symbols('a')
j = a*(a+1)*(a+2)
numList = factorint(j.subs(a, 1))
m = 1
for factNum in numList:
    m *= factNum * numList[factNum]
print("m =", m)
factorNum = factorint(6762)
print("6762 =", factorNum)
if 6762 % 49 == 0 and 6762 % 23 == 0 :
    minNumList = [49*23-2, 23*minAns-2, 49*minNum-1, 23*ans-2, 49*num]
    print("b =", min(minNumList))

出力

x= 8
y= 17
ク : 8
ケコ : 17
サ : 7
シス : 15
m = 6
6762 = {23: 1, 2: 1, 3: 1, 7: 2}
b = 343

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

第5問

根号で計算すると小数になる場合があるので、
2乗した数をわざと出している箇所があります。

プログラム

# -*- coding: utf-8 -*-

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

x = symbols('x')
y = symbols('y')
z = symbols('z')

ab = 4
bc = 7
ac = 5
cosBAC = -1/5
sinBAC = 2*sqrt(6)/5
S = 1/2*4*5*sinBAC
r = 2*S/(ab+bc+ac)
print("r =", r)
f = x+y-ab
g = y+z-bc
h = z+x-ac
print(f+g+h)
ad = solve(f+g+h, x)[0].subs(-y-z, -bc)
print("AD =", ad)
ae = ad
de = sqrt(ad**2+ae**2-2*ad*ae*cosBAC)
print("DE^2 =", de**2)

bd = ab - ad
ce = ac - ae
bq_qd = (bd/ad)*(ae/ce)
print("BQ/CQ =", bq_qd)
cq = solve((x+y-bc).subs(y, bq_qd*x), x)[0]
bq = bc-cq
print("BQ =", bq)
if bd == bq :
    iq = r
    print(iq)
    cosDFE = (ae**2+de**2-ad**2)/(2*ae*de)
    print("cosDFE^2 =", cosDFE**2)
else :
    print("iqは分からない")

出力

r = 0.5*sqrt(6)
2*x + 2*y + 2*z - 16
AD = 1
DE^2 = 2.40000000000000
BQ/CQ = 3/4
BQ = 3
0.5*sqrt(6)
cosDFE^2 = 0.600000000000000

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

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5