[PYTHON] Can I pass the first grade of math test by programming?

Introduction

This article deals with questions 1-5. (Out of 7 questions)

The difficulty level of Suken Level 1 is

University level / general level. According to the Japan Mathematical Examination Association, the pass rate is 5.7%, and all are descriptive. It is often said that it is more difficult than the entrance examination questions of the University of Tokyo.

Which problem to solve

I will try to solve some of the Math Test Level 1 Primary Exam Sample Questions. (The model answer is available here [https://www.su-gaku.net/digitalbook/past_question/1q_ans_1ji/index_h5.html#1].

Execution environment

Python......paiza.io FORTRAN ...... ABC187 code test in AtCoder code test

Let's actually solve

1st question

Question 1 Find the minimum value of the positive integer n that satisfies 2018n ≡ 2 mod (1000).

Will be. First of all, what is a mod?

What is mod When p ≡ q (mod n), p-q is divisible by n.

Did you understand somehow? Let's look at an example.

Example 3 ≡ 8(mod 5) 3-8 is divisible by 5.

It's a symbol like this.

I will solve this by full search (algorithm that tries all patterns). (Actually, the first digit is used for classification> the second digit is used for classification.)

Python

program
n = 1
while True:
    if (2018*n-2)%1000 == 0:
        break
    n += 1
    
print(n)
output
389

FORTRAN

program
program question1
  do n = 1,1000
    !Since it is mod 1000 here, we can prove that n does not take a value greater than 1001.
    if (mod((2018*n-2),1000) == 0) then
      print '(i0)', n
    end if
  end do
end
output
389
889

Therefore, the smallest 389 is considered to be the answer (although 1000 and 2018 are even numbers from the beginning, so a full search up to 500 is fine).

Swift

program
var n = 0
while n < 1001{
    if (2018*n-2)%1000 == 0{
        print(n)
        break
    }
    n += 1
}

It is an implementation in a while statement.

output
389

When I saw the answer, it was correct.

2nd question

Find tan (2 Arctan (1/3) + Arctan (1/12)). However, Arctan x represents the inverse function of tan x, where -π/2 <Arctan x <π/2.

What is "Arctan is the inverse function of tan"?

See this this article in detail.

I would like to calculate this normally and find a close value.

Python (low accuracy)

program
import math
inside = 2*math.degrees(math.atan(1/3))#Contents of tan
inside += math.degrees(math.atan(1/12))
print(math.tan(math.radians(inside)))
output
0.8888888888888887

Is it about 8/9 (correct answer)? I wonder if I really use Arctan's addition theorem ...

Python (precision UP)


Policy
--Let's realize Arctan's addition theorem ――Decimal numbers are prone to error, so let's think in fractions --Let's create a function that satisfies the above two conditions.

Wikipedia says:

\arctan u+\arctan v=\arctan \left({\frac  {u+v}{1-uv}}\right){\pmod  \pi },\qquad uv\neq 1\,.

Here, since it is defined only by -π/2 to π/2, (mod π) is omitted. Think of it as u = a/b, v = c/d. In the problem, after that, it is restored with tan, so only (u + v)/(1-uv) needs to be considered (tan (arctan (x)) = x). After all, the answer is (u + v)/(1-uv), so consider a program that outputs this.

For the time being, I made a function like this.

from fractions import Fraction
def arctan_sum(a,b,c,d):
    u = Fraction(a,b)
    v = Fraction(c,d)
    return ((u+v),(1-u*v))

The return value is a tuple. If you write as below,

program
from fractions import Fraction
def arctan_sum(a,b,c,d):
    u = Fraction(a,b)
    v = Fraction(c,d)
    return ((u+v),(1-u*v))
left = arctan_sum(1,3,1,3)
total = arctan_sum(left[0],left[1],1,12)
Output result
8/9

I got a beautiful answer.

3rd question

A tetrahedron with four points (1, -4,1), (2,2,2), (2, -6, -3), (3, -2, -1) in the xyz space as vertices Find the volume.

Solve using Salas's formula. First, create a function to find the volume of a tetrahedron whose vertices are (0,0,0) (x1, y1, z1), (x2, y2, z2), (x3, y3, z3). However, since we have to consider a tetrahedron with (0,0,0) as the apex, we would like to calculate x1 ~ z3 by hand (because it seems to be the fastest).

Python

function
from fractions import Fraction
def sarasu(x1,y1,z1,x2,y2,z2,x3,y3,z3):
    return Fraction(abs(y1*z2*x3+z1*x2*y3+x1*y2*z3-z1*y2*x3-x1*z2*y3-y1*x2*z3),6)
Whole program
from fractions import Fraction
def sarasu(x1,y1,z1,x2,y2,z2,x3,y3,z3):
    return Fraction(abs(y1*z2*x3+z1*x2*y3+x1*y2*z3-z1*y2*x3-x1*z2*y3-y1*x2*z3),6)
    
print(sarasu(1,6,1,1,-2,-4,2,2,-2))
Output result
3

This was also a simple answer. (It was the correct answer.)

4th question (1)


3rd order square matrix A= \left(
    \begin{array}{ccc}
      4 & 1 & 1 \\
      1 & 2 & 1 \\
      1 & 1 & 2
    \end{array}
  \right)Answer the following questions about.

① Find the eigenvalue.

Python You can find the eigenvalues ​​in Python, so I will ask for it.

program
import numpy as np
import numpy.linalg as LA

a = np.array([[4,1,1],[1,2,1],[1,1,2]])#Matrix for which you want to find eigenvalues


aeig_val = LA.eig(a)[0]
aeig_val = np.sort(aeig_val)

print(aeig_val)
Output result
[1. 2. 5.]

When I tried to list it, there was an error, so I made this result. (The answer is 1,2,5, so there is.)

4th question (1) Evolved version

I changed it to the form of the answer with replace. Python

Program (evolved version)
import numpy as np
import numpy.linalg as LA

a = np.array([[4,1,1],[1,2,1],[1,1,2]])#Matrix for which you want to find eigenvalues


aeig_val = LA.eig(a)[0]
aeig_val = np.sort(aeig_val)

ans = str(aeig_val)
ans = ans.replace("[","").replace(".]","").replace(". ",", ")

print(ans)
Output result
1, 2, 5

4th question (2)

Solving the eigenvalue problem numerically --I would like to find it by referring to Kochi University, Matrix power, 3 Identity matrix, Transpose of matrix (matrix). All you have to do is do this.

Python

program
import numpy as np
import numpy.linalg as LA

A = np.array([[4,1,1],[1,2,1],[1,1,2]])
I = np.array([[1,0,0],[0,1,0],[0,0,1]])

A3 = np.linalg.matrix_power(A, 3)#A^3
A2 = np.linalg.matrix_power(A, 2)#A^2

ans = A3-8*A2+18*A-12*I#Calculate with the operator as it is
print(ans)
Output result
[[2 1 1]
 [1 0 1]
 [1 1 0]]

If you write it in an easy-to-understand manner,


\left(
    \begin{array}{ccc}
      2 & 1 & 1 \\
      1 & 0 & 1 \\
      1 & 1 & 0
    \end{array}
  \right)

(This was also the correct answer)

5th question (1)

It is known that the number of machine failures within a certain period follows the Poisson distribution. One company uses two machines A and B, and when machine A follows an average of 2.5 failures and machine B averages 4.5 times in a year, answer the following questions. However, assuming that the number of failures of A and B in one year is independent of each other, the Poisson distribution table at 1-6-6 is used for the answer, and the answer is rounded off to the fourth decimal place and the third decimal place. Ask for.

① Find the probability that Machine A will fail more than 4 times a year.

The Poisson distribution is ...

"The probability function that represents the Poisson distribution is

P(k) = (e^{-k}*λ^k) / k!

is. In other words, the probability that a random event that occurs an average of λ times per unit time occurs k times per unit time is P (k). "

(Excerpt from Beautiful story of high school mathematics: Meaning and mean / variance of Poisson distribution)

In this problem, the probability of being broken less than 4 times is subtracted from the whole. At this time, it can be seen that λ is 2.5 and k is 0 to 3. Python

program
zero_three = 0.0821+0.2052+0.2565+0.2138#Just added

print(round((1-zero_three)*1000)/1000)
Output result
0.242

It was the correct answer.

5th question (2)

It is known that the number of machine failures within a certain period follows the Poisson distribution. One company uses two machines A and B, and when machine A follows an average of 2.5 failures and machine B averages 4.5 times in a year, answer the following questions. However, assuming that the number of failures of A and B in one year is independent of each other, the Poisson distribution table at 1-6-6 is used for the answer, and the answer is rounded off to the fourth decimal place and the third decimal place. Ask for.

② Find the probability that machines A and B will fail 8 times or more in total.

First, list the values ​​of the Poisson distribution with an average of 2.5 times and 4.5 times. Next, find the probability that the number of failures will be less than 8, and subtract it from the whole. Python

program
A = [0.0821,0.2052,0.2565,0.2138,0.1336,0.0668,0.0278,0.0099,0.0031,0.0009,0.0002]#Average 2.5 Poisson distribution
B = [0.0111,0.0500,0.1125,0.1687,0.1898,0.1708,0.1281,0.0824,0.0463,0.0232,0.0104]#Average 4.5 Poisson distribution

ans = 0
for i in range(8):
    for j in range(i+1):
        ans += A[i-j]*B[j]

print(round((1-ans)*1000)/1000)
Output result
0.401

It was the correct answer.

Conclusion

** Pass (5 questions on the pass line)! ** However, there is a problem that it is faster to calculate normally than to program.

References

-Qiita Markdown Writing Summary -Mathematics test, Arithmetic test homepage -Introduction to Japan NAG Fortran -Question about Yahoo Answers error -Tech Academy Iterative processing! How to use while/for statements written in Swift [for beginners] -Calculate trigonometric functions in Python (sin, cos, tan, arcsin, arccos, arctan) -Wikipedia Inverse Trigonometric Function -fractions --- rational numbers -LaTeX command collection matrix -Sort based on arbitrary row / column with NumPy sort and argsort functions --Solving the eigenvalue problem numerically --Kochi University -Matrix power -3 Identity matrix, matrix transpose (matrix) -Rounding decimals and integers in Python round and Decimal.quantize

Note

--This article does not recommend cheating on math tests. Please do not abuse it.

Recommended Posts

Can I pass the first grade of math test by programming?
I tried to verify the result of A / B test by chi-square test
I tried to pass the G test and E qualification by training from 50
I followed the implementation of the du command (first half)
I compared the identity of the images by Hu moment
Mitsuhiko "I can tell the criminal by introducing myself!"
I tried increasing or decreasing the number by programming
Why can I use the module by importing with python?
I tried programming the chi-square test in Python and Java.
What I saw by analyzing the data of the engineer market
The popularity of programming languages
I tried to solve the first question of the University of Tokyo 2019 math entrance exam with python sympy
I found out by analyzing the reviews of the job change site! ??
The first time a programming beginner tried simple data analysis by programming
I want to pass the G test in one month Day 1
Test the version of the argparse module
I investigated the mechanism of flask-login!
Get the first element of queryset
Setting to debug test by entering the contents of the library with pytest
I tried to find the optimal path of the dreamland by (quantum) annealing
I tried to verify and analyze the acceleration of Python by Cython
I checked the number of closed and opened stores nationwide by Corona