NumPy: Estimate π from random points

A simulation drew random points in the unit square. Turn them into an estimate of π and its standard error, as 4 * np.mean(x2 + y2 < 1) would.

What to build

A program that estimates π from given points and reports the uncertainty.

Requirements

  • Print inside K of N, the number of points with u² + v² < 1.
  • Print pi estimate E and standard error S, each to four decimals, where E = 4 × K ÷ N and S = 4 × √(p(1 − p) ÷ N) with p = K ÷ N.

Input

Line 1: N. Then N lines u v, numbers between 0 and 1.

Output

Three lines.

Constraints

1 ≤ N ≤ 10⁵.

Example

Input:

4
0.1 0.2
0.9 0.9
0.5 0.5
0.7 0.7

Output:

inside 3 of 4
pi estimate 3.0000
standard error 0.8660

Why it works

The quarter circle covers π ÷ 4 of the unit square, so the share of uniform points inside it estimates π ÷ 4, and the binomial standard error of that share, times 4, measures how far the estimate is likely to be from π.

Examples

Example 1 · Input
4
0.1 0.2
0.9 0.9
0.5 0.5
0.7 0.7
Expected output
inside 3 of 4
pi estimate 3.0000
standard error 0.8660
Example 2 · Input
2
0 0
1 0
Expected output
inside 1 of 2
pi estimate 2.0000
standard error 1.4142

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

A point is inside when u² + v² < 1.

Hint 2

The estimate is 4 × inside ÷ n.

Hint 3

The standard error of a share p is √(p(1 − p) ÷ n); multiply it by 4 as well.

Solution

Show one correct solution (try it yourself first)
n = int(input())
inside = 0
for _ in range(n):
    u, v = map(float, input().split())
    if u * u + v * v < 1:
        inside += 1
p = inside / n
print(f"inside {inside} of {n}")
print(f"pi estimate {4 * p:.4f}")
print(f"standard error {4 * (p * (1 - p) / n) ** 0.5:.4f}")

Lesson: Random numbers and simulation →