ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 09.03.2021

Просмотров: 240

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
background image

H

*

Вычисление числа

p

методом Монте

-

Карло

*

L

ClearAll

@

ntmax, n, m, x, y, pn, res

D

;

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

+

1

D

;

If

@

n

10

»»

n

100

»»

n

1000

»»

n

10000

»»

n

100000

»»

n

ntmax,

pn

=

PaddedForm

@

n, 9

D

;

res

=

PaddedForm

@

N

@

4

m

ê

n

D

,

8

13, 12

<D

;

Print

@

"n

=

", pn, "

res

=

", res

DDD

;

Print

@

"

Exact value of Pi

=

", PaddedForm

@

N

@

Pi, 17

D

,

8

13, 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, body

D

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, range

D

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

8

min, max

<

explicitly; a range specification of max is equivalent to

8

0, max

<

.

More…

? If

If

@

condition, t, f

D

gives t if condition evaluates to True, and f if it evaluates to False.

If

@

condition, t, f, u

D

gives u if condition evaluates to neither True nor False.

More…

? PaddedForm

PaddedForm

@

expr, n

D

prints with all numbers in expr padded to leave room for

a total of n digits. PaddedForm

@

expr,

8

n, f

<D

prints with approximate real

numbers having exactly f digits to the right of the decimal point.

More…

Pi.nb

1