LoginSignup
1
2

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-04-02
  • 実行環境
    • VirtualBox 5.2.2
    • Cent6.9
    • python3.5.4

そろそろ三角比を求める関数を作ったほうが楽な気がします。

第1問

(1)

a^3+b^3=(a+b)(a^2+b^2-ab)は明示としています。

プログラム

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

from sympy import *
import math
import numpy as np

x = symbols('x')
# 2/xの代わり
x_2 = symbols('x_2')

f = (x+2/x)**2
f_value = f.expand().subs(x**2+4/x**2, 9)
print("(x+2/x)^2 =", f_value)
g = x**3+x_2**3
g_value = g.factor().subs(x+x_2, sqrt(f_value)).subs(x**2+x_2**2, 9).subs(x_2, 2/x)
print("x^3+8/x^3 =", g_value)
h = x**4+x_2**4
h = h + 2*x**2*x_2**2
h = h.factor()
h = h - 2*x**2*x_2**2
h_value = h.subs(x**2+x_2**2, 9).subs(x_2, 2/x)
print("x^4+16/x^4 =", h_value)

出力

(x+2/x)^2 = 13
x^3+8/x^3 = 7*sqrt(13)
x^4+16/x^4 = 73

解答
ア:1、イ:3、ウ:2、エ:7、オ:1
カ:3、キ:7、ク:3

(2)

不十分ですが、xが1、-1の場合で条件分岐をしています。
反例が複数ある場合の処理をどうにかしたいです。

プログラム

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

from sympy import *
import math
import numpy as np

x = symbols('x')

if (x**2).subs(x, 1) == 1 :
    print("qはpの必要条件")

q_flag = False
p_not_q_flag = False
for num in solve(x**2-1, x) :
    if num != 1 :
        q_flag = False
        print("(not p and q)はqの十分条件")

    else :
        if q_flag :
            q_flag = True

not_p_q_flag = True
for num in solve(x**2, x) :
    if num != -1 :
        not_p_q_flag = False

if q_flag :
    print("qはpの十分条件")

if not_p_q_flag :
    print("(not p and q)はqの必要条件")

for num in solve(x**2-1, x) :
    if num == 1 :
        if num > 0 :
            print("(p and q) -> r is True")
        # こっちは通らないため不要であるが、考え方としては正しいため記述
        else :
            print("(p and q) -> r is False")


q_r_flag = True
for num in solve(x**2-1, x) :
    if num < 0 :
        print("q -> r is False")
        q_r_flag = False

if q_r_flag :
    print("q -> r is True")

if (x**2).subs(x, 1) == 1 :
    print("not q -> not p is True")
else :
    print("not q -> not p is False")

出力

qはpの必要条件
(not p and q)はqの十分条件
(p and q) -> r is True
q -> r is False
not q -> not p is True

解答
ケ:0、シ:1、ス:2
コ、サは出せていません。

(3)

2次方程式を微分で解いていくだけです。

プログラム

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

from sympy import *
import math
import numpy as np

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

g = x**2-2*(3*a**2+5*a)*x+18*a**4+30*a**3+49*a**2+16
gg = diff(g, x)
vertex_g_x = solve(gg, x)[0].expand()
vertex_g_y = g.subs(x, vertex_g_x).expand()
print("y=g(x)の頂点 = (%s, %s)" % (vertex_g_x, vertex_g_y))

min_vertex_g_x = vertex_g_x.subs(a, solve(diff(vertex_g_x, a), a)[0])
print("頂点のx座標の最小値 =", min_vertex_g_x)

vertex_g_y_t = vertex_g_y.subs(a**4, t**2).subs(a**2, t)
print("頂点のy座標 =", vertex_g_y_t)

num = 0
for tmp_num in solve(diff(vertex_g_y_t, t)) :
    if tmp_num > 0 :
        num = tmp_num

min_vertex_g_y = vertex_g_y.subs(a, sqrt(num))
print("頂点のy座標の最小値 =", min_vertex_g_y)

出力

y=g(x)の頂点 = (3*a**2 + 5*a, 9*a**4 + 24*a**2 + 16)
頂点のx座標の最小値 = -25/12
頂点のy座標 = 9*t**2 + 24*t + 16
頂点のy座標の最小値 = 16

解答
セ:3、ソ:5、タ:9、チ:2、ツ:4、テ:1、ト:6
ナ:2、ニ:5、ヌ:1、ネ:2、ノ:1、ハ:6

第2問

(1)

多項式が分母に来た時に展開がうまくできていないです。

プログラム

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

from sympy import *
import math
import numpy as np

sin_list={
     0: 0,
    30: 1/2,
    45: 1/sqrt(2),
    60: sqrt(3)/2,
    90: 1,
   120: sqrt(3)/2,
   135: 1/sqrt(2),
   150: 1/2
}

cos_list={
     0: 1,
    30: sqrt(3)/2,
    45: 1/sqrt(2),
    60: 1/2,
    90: 0,
   120: -1/2,
   135: -1/sqrt(2),
   150: -sqrt(3)/2
}

def getSin(angle) :
    return sin_list[angle]

def getCos(angle) :
    return cos_list[angle]

x = symbols('x')

ab = sqrt(3)-1
bc = sqrt(3)+1

ac = sqrt(int((ab**2+bc**2-2*ab*bc*getCos(60)).expand()))
print("AC =", ac)
r = ac/(2*getSin(60))
print("三角形ABCの外接円の半径 =", r)
sinBAC = (bc/(2*r)).expand()
print("sinBAC =", sinBAC)

ab_ad = ((sqrt(2)/6*2)/sinBAC).expand()
print(ab_ad)

出力

AC = sqrt(6)
三角形ABCの外接円の半径 = sqrt(2)
sinBAC = sqrt(2)/4 + sqrt(6)/4
sqrt(2)/(3*(sqrt(2)/4 + sqrt(6)/4))

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

(2)

散布図から分析するには画像を読み込ませるのがいいのでしょうか。
またグラフを読み取るのも手動でやっているので、どうにかしたいです。

プログラム

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

from sympy import *
import math
import numpy as np

d = symbols('d')
d_ave = symbols('d_ave')
s_d = symbols('s_d')

x = 1.80*(d-125.0)+60.0
x_ave = 1.80*(d_ave-125.0)+60.0

f = factor(x-x_ave)
print(f)
multiple_num = f.coeff(d-d_ave, 1)
print("Xの分散はDの分散の%s倍" % multiple_num**2)
print("XとYの共分散はDとYの共の分散の%s倍" % multiple_num)
s_x_y = multiple_num*s_d
s_x = sqrt(multiple_num**2)*s_d
rate = s_x_y/s_x
print("XとYの相関係数:DとYの相関係数 = %s:1" % rate)

# 1回目の最小値が108.0よりaが1回目
# 1回目
aData = {
  'min' : 108.0,
  'oneQuarter' : 115.5,
  'middle' : 124,
  'threeQuarter' : 129.5,
  'max' : 144
}

# 2回目
bData = {
  'min' : 104,
  'oneQuarter' : 110,
  'middle' : 114,
  'threeQuarter' : 125,
  'max' : 142
}

if aData['threeQuarter']-aData['oneQuarter'] > bData['threeQuarter']-bData['oneQuarter'] :
    print("0は正しい")

if aData['middle'] > bData['middle'] :
    print("1は正しい")

if aData['max'] < bData['max'] :
    print("2は正しい")

if aData['min'] < bData['min'] :
    print("3は正しい")

出力

1.8*(d - d_ave)
Xの分散はDの分散の3.24000000000000倍
XとYの共分散はDとYの共の分散の1.80000000000000倍
XとYの相関係数:DとYの相関係数 = 1.00000000000000:1
1は正しい

解答
ソ:4、タ:3、チ:2、ツ:0、テ:1
シスセは出せていません。

第3問

組合せも含めてプログラム上でできるようになるといいのですが。
排反事象の判断は手動です。

プログラム

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

import sympy as sym
import math

# あたりが1、はずれが0
lottery = (0, 0, 1, 1)
e1 = 1 - sym.Rational(lottery.count(1), lottery.__len__())*sym.Rational(lottery.count(1)-1, lottery.__len__()-1)
print("アイ :", e1)

e = sym.Rational(lottery.count(1), lottery.__len__()) * sym.Rational(lottery.count(1)-1, lottery.__len__()-1) * 3
print("カキ :", e)

p1 = e/e1
print("クケ :", p1)

e2 = sym.Rational(lottery.count(0), lottery.__len__()) + sym.Rational(lottery.count(1), lottery.__len__()) * sym.Rational(lottery.count(1)-1, lottery.__len__()-1) * 2
print("スセ :", e2)

e3 = 1 - sym.Rational(lottery.count(0), lottery.__len__()) * sym.Rational(lottery.count(1), lottery.__len__()-1) * sym.Rational(lottery.count(0)-1, lottery.__len__()-2)
print("ソタ :", e3)

p2 = e/e2
p3 = e/e3

print("p1 = %s, p2 = %s, p3 = %s" % (p1, p2, p3))

出力

アイ : 5/6
カキ : 1/2
クケ : 3/5
スセ : 5/6
ソタ : 5/6
p1 = 3/5, p2 = 3/5, p3 = 3/5

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

第4問

総当たりしているだけです。
こういった問題にはプログラムは強いと思います。
2進数変換も楽です。

プログラム

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

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

for a in range(0, 10) :
    if (370+a) % 4 == 0 :
        print("a =", a)

flag = True
numList = []
for b in range(0, 10) :
    for c in range(0, 10) :
        num = 7050+100*b+c
        if num % 4 == 0 and num % 9 == 0 :
            numList.append((b, c))
            if flag :
                print("min 7b5c =", num)
                flag = False

            print("b=%d, c=%d, n=%f" % (b, c, sqrt(num/36)))

print("max 7b5c = 7%d5%d" % (numList[-1][0], numList[-1][1]))
print("b,cの組:", len(numList))

divisorCnt = 1
if 1188 % 2 == 0 :
    doubleCnt  = 1
else :
    doubleCnt  = 0

if 1188 % 4 == 0 :
    fourthCnt  = 1
else :
    fourthCnt  = 0

prodictDivisor = 1188

# 1188の1/2までを確かめれば十分
for divisor in range(1, int(1188/2+1)) :
    if 1188 % divisor == 0 :
        divisorCnt += 1
        prodictDivisor *= divisor
        if divisor % 2 == 0 :
            doubleCnt += 1
            if divisor % 4 == 0 :
                fourthCnt += 1

print("1188の約数の個数:", divisorCnt)
print("1188の約数で2の倍数の個数:", doubleCnt)
print("1188の約数で4の倍数の個数:", fourthCnt)
print("1188の約数の積を2進数にすると:", bin(prodictDivisor))

出力

a = 2
a = 6
min 7b5c = 7056
b=0, c=6, n=14.000000
b=4, c=2, n=14.387495
b=9, c=6, n=14.866069
max 7b5c = 7956
b,cの組: 3
1188の約数の個数: 24
1188の約数で2の倍数の個数: 16
1188の約数で4の倍数の個数: 8
1188の約数の積を2進数にすると: 0b101111100100001010010111110011001101100001001110101110011101011100011011101011110000001001001100001000000000000000000000000

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

第5問

ことごとく小数になってしまっています。
分数で止めるとかできるんですかね。

プログラム

# -*- 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 = 3
bc = 8
ac = 7

ad = 3
cd = ac-ad

bc_ce = cd*ac
print("BC*CE =", bc_ce)

ce = bc_ce/bc
print("CE =", ce)

be = bc-ce
bf_af = (cd/ad)*(be/ce)
print("BF/AF =", bf_af)

af = ab/(bf_af-1)
print("AF =", af)

cosABC = (ab**2+bc**2-ac**2)/(2*ab*bc)
sinList = {30:1/2, 45:1/sqrt(2), 60:sqrt(3)/2, 90:1, 120:sqrt(3)/2, 135:1/sqrt(2), 150:1/2}
cosList = {30:sqrt(3)/2, 45:1/sqrt(2), 60:1/2, 90:0, 120:-1/2, 135:-1/sqrt(2), 150:-sqrt(3)/2}
for angle in cosList.keys() :
    if cosABC == cosList[angle] :
        angleABC = angle
        break

print("角ABC =", angleABC)
areaABC = 1/2*ab*bc*sinList[angleABC]
r = 2*areaABC/(ab+bc+ac)
print("三角形ABCの内接円の半径 =", r)

bi = r/sinList[angleABC/2]
print("BI =", bi)

出力

BC*CE = 28
CE = 3.5
BF/AF = 1.7142857142857144
AF = 4.199999999999999
角ABC = 60
三角形ABCの内接円の半径 = 0.666666666666667*sqrt(3)
BI = 1.33333333333333*sqrt(3)

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

1
2
2

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
1
2