Joke Collection Website - Mood Talk - You can also calculate pi-random layout algorithm-to the upcoming pi day.
You can also calculate pi-random layout algorithm-to the upcoming pi day.
As we know, it is said that Zu Chongzhi used the improved "tangent circle method" to calculate pi. Unfortunately, most of our modern brains can't understand this method. There are other complicated formulas, but the human brain is more difficult to understand, but there is a whimsical method, you know? Anyone can simply calculate pi (let's use pi instead of pi for writing and recognition,: p).
This method comes from the basis of probability theory and is called Monte Carlo method. If it is more vivid, we can also call it random placement. Let's talk about its principle first:
Let's take a look at the picture below.
Suppose there is a square with a circle in the middle. It can be seen intuitively that if the radius of the circle is r, the side length of the square is 2R.
According to the formula, the area of a circle is Pi times the square of r, that is, Pi × R × R = PiR?
According to the formula, the area of a square is the square of the side length, that is, (2R)×(2R)= 4R?
By dividing the two formulas, it is easy to calculate Pi = (area of circle ÷ area of square) × 4.
In this way, the problem of calculating pi value is subtly transformed into calculating the ratio of the area of a circle to that of a square that meets the above conditions.
At this time, probability theory can play a role. With computers, we have the weapon of "random number"!
Suppose we randomly draw a point in a square, then this point may fall inside or outside the circle; Then we repeat this action. From the perspective of probability theory, if it is done infinitely many times, the ratio of the number of points falling in the circle to all the points drawn should be the ratio of the area of the circle to the area of the square. See if the picture below is easy to understand?
Imagine that when there are enough points in it, you can cover the whole prototype and square. As the saying goes, "a point has a surface", which can be understood as the area of a circle and a square formed by countless points.
Ok, let's see the effect of calculating pi with computer program! We chose Go language (Golang) to implement this algorithm this time because Go language is faster (much faster than Python and Java) and easier to understand than C language.
The running result of this program is:
It can be seen that the calculated pi value is getting closer and closer to that well-known number: 3. 14 15. ...
Amazing, isn't it? Why do you say that people can figure it out? Imagine drawing a square and circle like that on the ground with chalk, and then let's throw sandbags into it at will! Is it very childlike?
- Previous article:My girlfriend went to the army and I was in pain.
- Next article:Ruthless sentences describing the years
- Related articles
- What are the sentences that describe the "deep affection between mother and daughter"? (62 sentences)
- Rural natural gas cannot be used for heating.
- Old stories, sentences, tell me about it.
- How do girls maintain their image? Tell me how you feel.
- 2022 positive energy is domineering and inspirational.
- Complete works of beautiful sentences describing spring rain
- What is the cause of breast duct dilation?
- Write a composition about the summer scenery of Dongzhou Park.
- Fifty original poems of Dragon Boat Festival
- How to dress in a French style?