[Confrontation! Human power vs Python] After all, which is faster, solving the mathematics of the center test with Python or solving it by yourself?

If you think it's something you've seen somewhere

After all, there was a predecessor (maybe there are others). **Great! ** **

1. What you can bring to the center test

It seems that you can bring in the following, but at the moment it seems that ** Python interpreter cannot be brought in </ font> **, so be careful. For developers, I think the claim that the development environment is like a pencil is ** not understandable **. ** * Never bring anything other than what is permitted! </ font> **

*Black pencil(Limited to H, F, HB. It is not possible to print Japanese poems, sayings, etc.), Pencil cap.
*mechanical pencil(Only available for memos and calculations, limited to black cores.)
*Plastic eraser
*Pencil sharpener(Electric, large and knives are not allowed.)
*clock(Those that have functions such as dictionaries, calculators, and terminals, those that are difficult to determine whether or not they have such functions, those that make a second hand sound, kitchen timers, and large ones are not allowed.)
*Glasses, handkerchiefs, eye drops, tissue paper(Only the contents are taken out from the bag or box.)

2. The problem to compete this time

** Only the first question [1] of the 2017 National Center Test for University Admissions Math II / Math B **.

Why I chose this question: "I think I can win this question in Python."

** Question 1 (Mandatory question) (Score 30) **

[1] Simultaneous equations

\left\{
\begin{array}{ll}
cos \, 2\alpha + cos \, 2\beta = \frac{4}{15} \quad\quad\quad\,\,\,\,...(1) \\
cos \, \alpha \, \, cos \, \beta = -\frac{2\sqrt{15}}{15}  \quad\quad\quad\,\,\,\,\,...(2)
\end{array}
\right.

think of. However, it is $ 0 \ leqq \ alpha \ leqq \ pi, \ quad 0 \ leqq \ beta \ leqq \ pi $, $ \ alpha <\ beta $ and

|cos \, \alpha| \geqq |cos \, \beta| \quad\quad\quad\quad\quad\quad...(3)

And. At this time, let's find the values of $ cos , \ alpha $ and $ cos , \ beta $.

Using the double-angle formula, from (1)

cos^2 \alpha + cos^2 \beta = \frac{[Ai]}{[Ue]}

Is obtained. Also, from (2),

cos^2 \alpha \, \, cos^2 \beta = \frac{[Oh]}{15}

Is. Therefore, using condition (3)

cos^2 \alpha = \frac{[Mosquito]}{[Ki]} \quad, \quad\quad cos^2 \beta = \frac{[Ku]}{[Ke]}

Is. Therefore, from the condition of (2) $ 0 \ leqq \ alpha \ leqq \ pi, \ quad 0 \ leqq \ beta \ leqq \ pi, \ quad \ alpha <\ beta $

cos \, \alpha = \frac{[Ko]\sqrt{[Service]}}{[Shi]} \quad, \quad\quad cos \, \beta = \frac{[Su]\sqrt{[Se]}}{[So]}

Is.

3. Victory conditions on the "Python + me" side

Given the total amount of the problem, I think this problem is tough unless it is solved within 6 or 7 minutes. With that in mind, the victory conditions on the "Python + me" side are set as follows.

  • The total time required for coding and execution is within 5 minutes.
  • It takes less than 3 minutes, no, less than 2 minutes to reach a level that can be said to be a complete victory.
  • Python development environment (including IDE) may be pre-installed and started.

The environment prepared for reference is as follows.

  • Windows 10 Pro
  • Python 3.6
  • Visual Studio Code + Python Extension installed

4. It's always a match!

(The entire completed code will be posted at the end of this article.)

4.1. Ignore the guidance of the questioner from the beginning!

I'm saying that I don't understand the double-angle formula, so Suddenly, it is calculated from the last $ cos \ alpha, cos \ beta $. You should be able to do it with Python and SymPy. First, install SymPy!

pip install sympy

Let's solve it all at once!

f_1 = cos \, 2\alpha + cos \, 2\beta - \frac{4}{15} f_2 = cos \, \alpha \, \, cos \, \beta + \frac{2\sqrt{15}}{15} As a system of equations of $ f_1 = 0 $ and $ f_2 = 0 $, We will solve for $ cos , \ alpha $ and $ cos , \ beta $.

from sympy import symbols, expand, cos, Rational, sqrt, solve

#Symbol definition. In this problem, α and β are treated as symbols of a and b, respectively.
a, b = symbols('a b')

# (1)When(2)Each formula is f1= 0, f2 = 0Whenなるような関数f1, f2Whenして定義。
#After this, cos(a)And cos(b)Since we solve the simultaneous equations with respect to(1)Deploy(expand)I will do it.
f1 = expand(cos(2*a) + cos(2*b) - Rational(4, 15), trig = True)
f2 = cos(a) * cos(b) + 2 * sqrt(15) / 15

#Simultaneous equations of f1 and f2, cos(a)And cos(b)Solve for(solve)。
answers = solve([f1, f2], [cos(a), cos(b)])

4.2. Narrow down to the desired solution from the conditions.

At this point, ʻanswers` should have some solutions, but find the one you want from the conditions.

$ 0 \ leqq \ alpha \ leqq \ pi, \ quad 0 \ leqq \ beta \ leqq \ pi $, with the condition $ \ alpha <\ beta $ In the range of $ 0 \ leqq \ theta \ leqq \ pi $, $ cos \ theta $ becomes smaller as $ \ theta $ becomes larger, so Find a solution that is $ cos \ alpha> cos \ beta . And,(3)Conditions|cos , \alpha| \geqq |cos , \beta|$Also evaluate to find the desired solution.

Since the absolute value has come out, I will add it to ʻimport`.

from sympy import symbols, expand, cos, Rational, sqrt, solve, Abs

After that, we will search for the set of solutions that meet the conditions from the list of sets of solutions contained in ʻanswers. In the ʻif statement, the condition is simply made into a conditional expression.

# 0 <= a <= π, 0 <= b <=In π, a,The larger b is cos(a), cos(b)The value of becomes smaller.
# a <b, so cos(a) > cos(b)Look for something that will be.
#And,(3)Find the one that meets the conditions of.
for cos_a, cos_b in answers:
    if cos_a > cos_b and Abs(cos_a) >= Abs(cos_b):
        break

4.3. [Ko] [Sa] [Shi] [Su] [Se] [So] capture completed!

This will give you $ cos \ alpha, cos \ beta $.

#Target cos(a)And cos(b)I got it, so I will display it([Ko][Service][Shi][Su][Se][So]Answer)。
print("[Kosashi] -> {}, [Suseso] -> {}".format(cos_a, cos_b))

4.4. The rest is easy!

Just ask for $ cos ^ 2 \ alpha, cos ^ 2 \ beta $ and $ cos ^ 2 \ alpha + cos ^ 2 \ beta $, $ cos ^ 2 \ alpha , , cos ^ 2 \ beta $ Since $ cos \ alpha and cos \ beta $ have already been calculated, they can be calculated simply by calculating.

# cos(a)And cos(b)Find the square of each.
squared_cos_a = cos_a ** 2
squared_cos_b = cos_b ** 2

# cos(a)Squared and cos(b)Shows the square of([Mosquito][Ki][Ku][Ke]Answer)。
print("[Oyster] -> {}, [Kuke] -> {}".format(squared_cos_a, squared_cos_b))

# cos(a)Squared and cos(b)Also displays the sum and product of the squares of([A][I][C][D][Oh]Answer)。
print("[Aiue] -> {}".format(squared_cos_a + squared_cos_b))
print("[Oh] -> {}".format(squared_cos_a * squared_cos_b))

(The entire completed code will be posted at the end of this article.)

5. Win or lose

The time it takes to execute the completed Python code (the time it takes to find all the solutions) is In my environment, it was ** less than 400ms ** (just the time since the code started running).

However, the time required for coding was ** 14 minutes 41 seconds 524 **, so It was a complete defeat on the Python side (or rather me), ** overwhelming defeat **. .. ..

(Since this article itself started writing after the game was settled, the writing time of the article is not included.)

5.1. Analysis of the cause of defeat

  1. Immature programming ability.
  2. I wrote a comment seriously.
  3. It was a little difficult to implement the solution narrowing down after solving the simultaneous equations while using the debugger.
  4. It took longer than I expected to install SymPy.

6. Completed code

from sympy import symbols, expand, cos, Rational, sqrt, solve, Abs
import time

#I want to output the time it took to solve, so remember the first time.
offset = time.time()

#Symbol definition. In this problem, α and β are treated as symbols of a and b, respectively.
a, b = symbols('a b')

# (1)When(2)Each formula is f1= 0, f2 = 0Whenなるような関数f1, f2Whenして定義。
#After this, cos(a)And cos(b)Since we solve the simultaneous equations with respect to(1)Deploy(expand)I will do it.
f1 = expand(cos(2*a) + cos(2*b) - Rational(4, 15), trig = True)
f2 = cos(a) * cos(b) + 2 * sqrt(15) / 15

#Simultaneous equations of f1 and f2, cos(a)And cos(b)Solve for(solve)。
answers = solve([f1, f2], [cos(a), cos(b)])

# 0 <= a <= π, 0 <= b <=In π, a,The larger b is cos(a), cos(b)The value of becomes smaller.
# a <b, so cos(a) > cos(b)Look for something that will be.
#And,(3)Find the one that meets the conditions of.
for cos_a, cos_b in answers:
    if cos_a > cos_b and Abs(cos_a) >= Abs(cos_b):
        break

#Target cos(a)And cos(b)I got it, so I will display it([Ko][Service][Shi][Su][Se][So]Answer)。
print("[Kosashi] -> {}, [Suseso] -> {}".format(cos_a, cos_b))

# cos(a)And cos(b)Find the square of each.
squared_cos_a = cos_a ** 2
squared_cos_b = cos_b ** 2

# cos(a)Squared and cos(b)Shows the square of([Mosquito][Ki][Ku][Ke]Answer)。
print("[Oyster] -> {}, [Kuke] -> {}".format(squared_cos_a, squared_cos_b))

# cos(a)Squared and cos(b)Also displays the sum and product of the squares of([A][I][C][D][Oh]Answer)。
print("[Aiue] -> {}".format(squared_cos_a + squared_cos_b))
print("[Oh] -> {}".format(squared_cos_a * squared_cos_b))

#Shows the time it took to solve.
elapsed = time.time() - offset
print('Time taken to solve: {0:.4f}millisecond'.format((elapsed * 1000)))

Recommended Posts