algorithm - Can a Monte Carlo pi calculation be used for a world record? -


i have random function calculate pi monte carlo style:

enter image description here

max=10000000; format long;  in = 0; tic k=1:max     x = rand();     y = rand();     if sqrt(x^2 + y^2) < 1         in = in + 1;     end end  toc calc_pi = 4*in/max  epsilon = abs(pi - calc_pi)  calcpi(100000000); 

if iterate 10e100 times, algorithm compete the world record? if so, how can find number of iteration give nth digit?

this nice exercise calculating pi, inefficient one. remarks:

  • my statistics rusty before had coffee, guess error scales 1 / sqrt(n_guess). n digits, need error of 10^(-n), need (10^n)^2 random guesses. if 1e100 guesses, proposed, on order of 50 digits of pi! number of iteration exponentional function of number of required digits, horribly slow. algorithm maybe linear in number of digits want.

  • with large number of guesses required, have start questioning quality of random number generator.

  • your algorithm limited floating-point errors 1e-16 or so. calculating digits of pi requires sort of arbitrary precision number format.

  • to speed algorithm, can leave out sqrt().

  • don't use variable called max, overwrites existing function. use n_guess or so.

quick , dirty test prove theory (after coffee):

pie = @(n) 4 * nnz(rand(n,1).^2 + rand(n, 1).^2 < 1) / n; ntrial = round(logspace(1, 8, 100)); pies = arrayfun(pie, ntrial);  loglog(ntrial, abs(pies - pi), '.k', ntrial, ntrial.^-.5, '--r') xlabel('ntrials') ylabel('epsilon') legend('monte carlo', '1 / sqrt(ntrial)') 

monte carlo result


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -