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.
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, 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 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} **!
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