Try to solve a set problem of high school math with Python

About this article

This article is "Let's solve a simple problem of a set of high school math with Python!". It's pretty rudimentary to do, and it's easier to do it directly than to do it in Python, but I want to keep the fun of doing various things in Python (mainly for myself), so I'll do it. I will.

Review about symbols

Hereinafter, A and B are set.

・ ** A∪B **: What is called a union. A set of all elements belonging to at least one of A and B.

・ ** A∩B **: What is called an intersection. A set of all elements that belong to both A and B.

ex) When A = {1,2,3,4,5} B = {2,4,6}, A∪B = {1,2,3,4,5,6}, A∩B = { 2,4}.

In the range of sets, it is often the case that one set is defined in advance and then a subset of that set is considered. At that time, the predetermined set is called ** whole set ** and is often placed in U.

_ -** A **: What is called a complement. For a subset A of the whole set U, the whole set of elements of U that do not belong to A. In this article, I will write A_c etc. for convenience. Excuse me.

ex) When U = {x | x is a natural number less than 10} A = {2,4,6,8}, A_c = {1,3,5,7,9}

In python

In Python, write as follows.

set.py


>>>A | B  #A,Create a new aggregate variable with all the elements contained in B. That is, the union of A and B.
>>>A & B  #A,Create a new set variable with elements commonly contained in B. That is, the intersection of A and B.
>>>A - B  #Create a new aggregate variable with elements that are included in A but not in B. That is, the complement of B when A is the whole set.

Let's solve the problem

Let's solve the problem immediately. In the following, A∩B may be written as capAB and A∪B may be written as cupAB. Please note.

** 1 **. Find A∩B, A∪B for the following sets A and B. (1)A = {1,2,3,4,5} , B = {2,3,5,7}

1-1.py


>>>A={1,2,3,4,5}
>>>B={2,3,5,7} #First, let's define sets A and B.
>>>capAB = A & B #From the above, if you want to find the common part&Is used.
>>>capAB
{2, 3, 5}
>>>cupAB = A | B #From the above, if you want to find the union|Is used.
>>>cupAB
{1, 2, 3, 4, 5, 7}

So the answer is ** A∩B = {2,3,5} **, ** A∪B = {1, 2, 3, 4, 5, 7} **!

(2)A = {x|x is a positive divisor of 24} , B = {x|x is a positive divisor of 32}

1-2.py


>>>A={1,2,3,4,6,8,12,24}
>>>B={1,2,4,8,16,32}
>>>capAB = A & B
>>>capAB
{8, 1, 2, 4}
>>>cupAB = A | B
>>>cupAB
{32, 1, 2, 3, 4, 6, 8, 12, 16, 24}

So the answer is ** A∩B = {1,2,4,8} **, ** A∪B = {1,2,3,4,6,8,12,16,24,32} * * Will be!


** 2 **. A = {1,2,3,4,5,6}, B = {2,4,6,8,10}, C = {1,2,4,8,16} Then, find A∩B∩C, A∪B∪C.

2.py


>>>A={1,2,3,4,5,6}
>>>B={2,4,6,8,10}
>>>C={1,2,4,8,16}
>>>X = A & B
>>>capXC = X & C
>>>capXC
{2, 4}
>>>Y = A | B
>>>cupYC = Y | C
>>>cupYC
{1, 2, 3, 4, 5, 6, 8, 10, 16}

So the answer is ** A∩B∩C = {2,4} **, ** A∪B∪C = {1,2,3,4,5,6,8,10,16} ** I will!


** 3 **. Find the following set for U = {x | x is a natural number less than 10}, A = {2,4,6}, B = {1,3,4,7}. (1)A_c

3-1.py


>>>U={1,2,3,4,5,6,7,8,9,10},
>>>A={2,4,6}
>>>A_c = U - A #From the above, when finding the complement-Is used.
>>>A_c
{1, 3, 5, 7, 8, 9, 10}

So the answer is ** A_c = {1,3,5,7,8,9,10} **!

(2)A∩(B_c)

3-2.py


>>>U={1,2,3,4,5,6,7,8,9,10}
>>>A={2,4,6}
>>>B={1,3,4,7}
>>>B_c= U - B
>>>capAB_c = A & B_c
>>>capAB_c
{2, 6}

So the answer is ** A∩B_c = {2,6} **!

(3)(A_c)∪(B_c)

3-3.py


>>>U={1,2,3,4,5,6,7,8,9,10}
>>>A={2,4,6}
>>>B={1,3,4,7}
>>>A_c = U - A
>>>B_c= U - B
>>>cupA_cB_c = A_c | B_c
>>>cupA_cB_c
{1, 2, 3, 5, 6, 7, 8, 9, 10}

So the answer is ** (A_c) ∪ (B_c) = {1,2,3,5,6,7,8,9,10} **!

(4)(A∩B)_c

3-4.py


>>>U={1,2,3,4,5,6,7,8,9,10}
>>>A={2,4,6}
>>>B={1,3,4,7}
>>>capAB = A & B
>>>c_capAB = U - capAB
>>>c_capAB
{1, 2, 3, 5, 6, 7, 8, 9, 10}

So the answer is ** (A∩B) _c = {1,2,3,5,6,7,8,9,10} **!

Finally

That is the end. In Q1-2 etc., there was a scene where divisors were elements of the set. Therefore, I think that the process of finding the divisor can be done in python, so I would like to update it when I have a free hand. If you have any mistakes, please report them. Thank you very much.

Recommended Posts

Try to solve a set problem of high school math with Python
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
A collection of competitive pro techniques to solve with Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Try to solve the N Queens problem with SA of PyQUBO
I wanted to solve the ABC164 A ~ D problem with Python
Try to solve the fizzbuzz problem with Keras
Try to calculate a statistical problem in Python
Try to solve the Python class inheritance problem
Try to solve the man-machine chart with Python
Try to draw a life curve with python
Try to make a capture software with as high accuracy as possible with python (2)
Try to make a "cryptanalysis" cipher with Python
Solve A ~ D of yuki coder 247 with python
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
Try to make a dihedral group with Python
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Try to solve the traveling salesman problem with a genetic algorithm (execution result)
Try to solve the programming challenge book with python3
A memo connected to HiveServer2 of EMR with python
Try to make a command standby tool with python
The 15th offline real-time I tried to solve the problem of how to write with python
Try to bring up a subwindow with PyQt5 and Python
Try to automate the operation of network devices with Python
Solve ABC163 A ~ C with Python
Try to operate Facebook with Python
Solve ABC166 A ~ D with Python
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
[High school mathematics + python] Logarithmic problem
Solve ABC158 A ~ C with Python
Try to get a list of breaking news threads in Python.
Try to create a python environment with Visual Studio Code & WSL
Try to extract a character string from an image with Python3
I tried to create a list of prime numbers with python
Solve the Python knapsack problem with a branch and bound method
Try to solve the shortest path with Python + NetworkX + social data
Solve the subset sum problem with a full search in Python
Try adding a wall to your IFC file with IfcOpenShell python
I tried to solve the first question of the University of Tokyo 2019 math entrance exam with python sympy
Try logging in to qiita with Python
Stack problem: Try to solve "20. Valid Parentheses"
Try to make a kernel of Jupyter
I wanted to solve ABC160 with Python
Solve AtCoder ABC168 with python (A ~ D)
Try HTML scraping with a Python library
Try drawing a map with python + cartopy 0.18.0
I wanted to solve ABC172 with Python
The 16th offline real-time how to write reference problem to solve with Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Try to set up a Vim test environment quite seriously (for Python)
A beginner of machine learning tried to predict Arima Kinen with python
The 19th offline real-time how to write reference problem to solve with Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
How to download all photos of egao school photo service with python base
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
Try to beautify with Talking Head Anime from a Single Image [python preparation]
Combinatorial optimization --Typical problem-- Partition of a set problem