Math 242 A, 21 February 2022

Look at the output from the Salamin-Brent code:

piValues=salaminBrent/@Range[13]

piValues[[2]]

$MaxExtraPrecision=50000
accuracy=N[-Log[10,Abs[Pi-piValues]],5]

Measure runtime of 13 iterations of Salamin-Brent:

ClearSystemCache[];
Timing[salaminBrent[13]][[1]]
Timing[N[salaminBrent[13],11176]][[1]]

ClearSystemCache[];
Timing[salaminBrent[14]][[1]]
Timing[N[salaminBrent[14],22000]][[1]]

Dart Board

Dart bord algorithm: throw m darts at a square of side length 2. Then compute how many are inside a circle inscribed in the square.

dartBoard[m_]:=Module[{x,y,acc=0},
Do[
(* generate a random point (x,y) *)
x=RandomReal[];
y=RandomReal[];
(*Print[{x,y}];*)

(* check if the dart is inside the circle *)
If[x^2+y^2<1,acc+=1]
,m];

(* return value *)
4*acc/m
]

Try it out:

dartBoard[100]

N[%]

Table[RandomReal[],10]

How many darts (m) are required to get an estimate that is within 0.1 of π at least 75% of the time?

vals=Table[dartBoard[800],100]

Histogram[vals,20]

closeToPi=Select[vals,Abs[#-Pi]<0.1&]

Length[closeToPi]