Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.

I'm reviewing high school mathematics, so I tried to solve it using Ruby and Python.

Venn diagram

ベン図.png

solve

Assuming that the sample space S = {1, 2, 3, ..., 98, 99, 100}, the multiple of 3 is event A, and the multiple of 5 is event B, the multiple of 3 or 5 Cards that are multiples of

E1 = A \cup B

--n (A) = multiples of 3 = 100/3 = 33 sheets --n (B) = multiples of 5 = 100/5 = 20 sheets --n (A \ cap B) Multiples of 3 and multiples of 5 = Numbers of multiples of 15 = 100/15 = 6 sheets

So

n(E1) = n(A \cup B)
= n(A) + n(B) - n(A \cap B)
= 33 + 20 - 6
= 47

So a card that is a multiple of 3 but not a multiple of 5

n(E) = n(E1) - n(B)
= 47 - 20
= 27

The probability of drawing a card that is a multiple of 3 but not a multiple of 5

n(E) / n(S)
= 27/100

Write in Ruby

The union can be calculated with |. The intersection is &.

> s = (1..100).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
> a = []
=> []
> s.to_a.each{|i|
* a << i if i %3 == 0
> }
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
> b = []
=> []
> s.to_a.each{|i|
* b << i if i % 5 == 0
> }
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
> e1 = a | b
=> [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 5, 10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95, 100]
> e = e1.length - b.length
=> 27
> e.to_f / 100
=> 0.27

Write in python

The union can be calculated with the union () function. The intersection is the interrupt () function.

>>> a = set(range(3,101,3))
>>> b = set(range(5,101,5))
>>> e1 = a.union(b)
>>> e = len(e1) - len(b)
>>> float(e)/100
0.27

Addendum) The union can also be calculated with |.

>>> a = set(range(3,101,3))
>>> b = set(range(5,101,5))
>>> e1 = a | b
>>> e = len(e1) - len(b)
>>> float(e)/100
0.27

I also tried using Sympy.

>>> from sympy import FiniteSet
>>> ma = range(3,101,3)
>>> mb = range(5,101,5)
>>> a = FiniteSet(*ma)
>>> b = FiniteSet(*mb)
>>> e1 = a.union(b)
>>> e = len(e1) - len(b)
>>> float(e)/100
0.27

range () function Easy.

reference

-Qiita Formula Cheat Sheet -[Python aggregate type](http://kaworu.jpn.org/python/Python aggregate type) -Introduction to Mathematics Starting with Python (Book)

Recommended Posts

Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
Find out the name of the method that called it from the method that is python
[Shell art] Only when it is a multiple of 3 and a number with 3 becomes stupid
There is a pattern that the program did not stop when using Python threading
A story that makes it easy to estimate the living area using Elasticsearch and Python
[Python] A program to find the number of apples and oranges that can be harvested
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
How to deal with the problem that the current directory moves when Python is executed from Atom
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python
Minimum knowledge required when dealing with "angles" and "coordinates" in Ruby + In what direction is Mr. B from the perspective of Mr. A? Algorithm
[Completed version] Try to find out the number of residents in the town from the address list with Python
With PEP8 and PEP257, Python coding that is not embarrassing to show to people!
[CleanArchitecture with Python] Apply CleanArchitecture step by step to a simple API and try to understand "what kind of change is strong" in the code base.
I thought it was the same as python, and I was addicted to the problem that the ruby interpreter did not start.
Competitive programming solution using that the product of the least common multiple of a and b and the greatest common divisor is ab
Prepare a development environment that is portable and easy to duplicate without polluting the environment with Python embeddable (Windows)
How to correctly upgrade the software when building Linux (CentOS) with Vagrant ~ Using the example of upgrading from Python 2.7 to Python 3.6 ~
It seems that the version of pyflakes is not the latest when flake8 is installed
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Python --Read data from a numeric data file and find the multiple regression line.
When using PyQtGraph with Python Pyside, pay attention to the order of import
WEB scraping with python and try to make a word cloud from reviews
[Python Data Frame] When the value is empty, fill it with the value of another column.
From the initial state of CentOS8 to running php python perl ruby with nginx
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
Try to create a battle record table with matplotlib from the data of "Schedule-kun"
Perform a Twitter search from Python and try to generate sentences with Markov chains.
Extract images and tables from pdf with python to reduce the burden of reporting
How to check in Python if one of the elements of a list is in another list
How to find out which process is using the localhost port and stop it
The timing when the value of the default argument is evaluated is different between Ruby and Python.
A memo of misunderstanding when trying to load the entire self-made module with Python3
Try to create a waveform (audio spectrum) that moves according to the sound with python
It is easy to execute SQL with Python and output the result in Excel
Find the part that is 575 from Wikipedia in Python
I tried the python version of "Consideration of Conner Davis's answer" Printing numbers from 1 to 100 without using loops, recursion, and goto "
Collect tweets about "Corona" with python and automatically detect words that became a hot topic due to the influence of "Corona"
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
A script that pings the registered server and sends an email with Gmail a certain number of times when it fails
I wrote a class that makes it easier to divide by specifying part of speech when using Mecab in python
A story that I did not know how to load a mixin when making a front with the django app [Beginners learn python with a reference book in one hand]
Get the matched string with a regular expression and reuse it when replacing on Python3
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
A library that monitors the life and death of other machines by pinging from Python
A learning roadmap that allows you to develop and publish services from scratch with Python
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Recursively get the Excel list in a specific folder with python and write it to Excel.
I made a server with Python socket and ssl and tried to access it from a browser
How to find the average amount of information (entropy) of the original probability distribution from a sample
Return the image data with Flask of Python and draw it to the canvas element of HTML
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
[Python beginner] Variables and scope inside the function (when the processing inside the function is reflected outside the function and when it is not reflected)
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
A Python script that allows you to check the status of the server from your browser
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
Try to visualize the nutrients of corn flakes that M-1 champion Milkboy said with Python
Precautions when inputting from CSV with Python and outputting to json to make it an exe