Estimate Pi from Uniform Samples
Problem statement
You are given independent points already sampled uniformly from the unit square [0,1] x [0,1]. Each row of samples contains one point [x,y].
Estimate pi as four times the fraction of points on or inside the quarter unit circle: x*x + y*y <= 1.
Function
estimatePi(samples: double[][]) → doubleExamples
Example 1
samples = [[0.0,0.0],[1.0,1.0],[0.5,0.5],[0.9,0.1]]return = 3.0Three of four points lie in the quarter circle, so the estimate is 4 * 3 / 4 = 3.
Example 2
samples = [[1.0,0.0],[0.0,1.0],[1.0,1.0]]return = 2.6666666666666665Boundary points are included, so two of the three points count.
Constraints
1 <= samples.length <= 100000samples[i].length = 20 <= samples[i][j] <= 1- Results use absolute tolerance
1e-12