If you want to solve the following puzzle then it may be not so easy/intuitive at a glance. It is a square and 20 is the area with e.g. unit = square meters (which really does not matter)
The computer is very good at this if you want to get a very close approximation, you can use the Monte-Carlo algorithm. The principle of Monte-Carlo is to generate as many random samples (in this case points) as possible (however definitely) then count the number of points that fall inside the shadow region. The ratio multiples the area of the rectangle equals the area of the shadow region. The following VBScript demonstrates this algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ' HelloACM.com Const N = 1000000 C = 0 Randomize For i = 1 To N x = Rnd y = Rnd r = Sqr(x * x + y * y) If (r >= 1) Then ' Outside the 1/4 Circle r = Sqr((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) If (r <= 0.5) Then ' Distance to Center smaller than Radius = 0.5 C = C + 1 End If End If Next Msgbox C / N * 20 |
' HelloACM.com Const N = 1000000 C = 0 Randomize For i = 1 To N x = Rnd y = Rnd r = Sqr(x * x + y * y) If (r >= 1) Then ' Outside the 1/4 Circle r = Sqr((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) If (r <= 0.5) Then ' Distance to Center smaller than Radius = 0.5 C = C + 1 End If End If Next Msgbox C / N * 20
We assume the each side of rectangle is 1 so the radius of the circle is 0.5. We first exclude the points that fall inside the 1/4 circle and then we count the points that only fall inside the full circle by using distance to center (less than 0.5). The ratio can be obtained by dividing the number to total samples.
Obviously, the more samples we have, more accurate this result is. I leave this to you guys as a C++ coding exercise.
Pure Math Solution (by Josué Molina)
Monte Carlo Simulation Algorithms to compute the Pi based on Randomness:
- Teaching Kids Programming – Area and Circumferences of Circle and Monte Carlo Simulation Algorithm of PI
- Using Parallel For in Java to Compute PI using Monte Carlo Algorithm
- Monte Carlo solution for Mathematics × Programming Competition #7
- R Programming Tutorial – How to Compute PI using Monte Carlo in R?
- C++ Coding Exercise – Parallel For – Monte Carlo PI Calculation
- Area of the Shadow? – The Monte Carlo Solution in VBScript
- VBScript Coding Exercise – Compute PI using Monte Carlo Random Method
- Computing Approximate Value of PI using Monte Carlo in Python
- How does the 8-bit BASIC perform on Famicom Clone – Subor SB2000 – FBasic – Compute PI approximation using Monte-Carlo method
- GoLang: Compute the Math Pi Value via Monte Carlo Simulation
- BASH Script to Compute the Math.PI constant via Monte Carlo Simulation
--EOF (The Ultimate Computing & Technology Blog) --
loading...
Last Post: Manage WordPress Connections, Settings in One Place for Multiple WordPress Sites on Same Domain
Next Post: Google Talk supports (y)