int-1 / Pi_Mathematica
.pdfPi.nb |
1 |
|
|
H*
Вычисление числа P методом Монте-Карло
*L
ClearAll@ntmax, n, m, x, y, pn, resD;
ntmax = 1000000;
m = 0;
For@n = 1, n ≤ ntmax, n = n + 1, x = Random@D; y = Random@D; If@x x + y y ≤ 1, m = m + 1D;
If@n 10 »» n 100 »» n 1000 »» n 10000 »» n 100000 »» n ntmax,
pn = PaddedForm@n, 9D;
res = PaddedForm@N@4 m ê nD, 813, 12<D;
Print@"n=", pn, " res=", resDDD;
Print@" Exact value of Pi= ", PaddedForm@N@Pi, 17D, 813, 12<DD;
n= |
10 |
res= 4.000000000000 |
n= |
100 |
res= 3.320000000000 |
n= |
1000 |
res= 3.160000000000 |
n= |
10000 |
res= 3.164000000000 |
n= |
100000 |
res= 3.131480000000 |
n= |
1000000 |
res= 3.139484000000 |
Exact value of Pi= 3.141592653590
? For
For@start, test, incr, bodyD executes start, then
repeatedly evaluates body and incr until test fails to give True. More…
? Random
Random@ D gives a uniformly distributed pseudorandom Real in the range
0 to 1. Random@type, rangeD gives a pseudorandom number of the specified type, lying in the specified range. Possible types are: Integer, Real and Complex. The default range is 0 to 1. You can give the range 8min, max< explicitly; a range specification of max is equivalent to 80, max<. More…
? If
If@condition, t, fD gives t if condition evaluates to True, and f if it evaluates to False. If@condition, t, f, uD gives u if condition evaluates to neither True nor False. More…
? PaddedForm
PaddedForm@expr, nD prints with all numbers in expr padded to leave room for a total of n digits. PaddedForm@expr, 8n, f<D prints with approximate real numbers having exactly f digits to the right of the decimal point. More…