my old versions:
use Leibniz formula for Pi
(defun Leibniz_pi (/ time)
;; ElpanovEvgeniy
;; Calculation of Pi
;; Last edit: 25.04.2006
;; (Leibniz_pi)
;; >> Pi = 3.1415926 spent time 72.056 sec.
;; use Leibniz formula for Pi
;| (+ (- (/ 4 1.) (/ 4 3.))
(- (/ 4 5.) (/ 4 7.))
(- (/ 4 9.) (/ 4 11.))
(- (/ 4 13.) (/ 4 15.))
(- (/ 4 17.) (/ 4 19.))
(- (/ 4 21.) (/ 4 23.))
...
)|;
(defun rec-pi_1 (i0)
(if (< i i0)
(+ (- (/ 4. i) (/ 4. (+ 2. i))) (rec-pi_1 (progn (setq i (+ 4. i)) i0)))
0.
) ;_ if
) ;_ defun
(defun rec-pi (i)
(if (< i 99999990)
(+ (rec-pi_1 (+ i 10000.)) (rec-pi i))
0.
) ;_ if
)
(setq time (car (_VL-TIMES)))
(princ (strcat "Pi = "
(rtos (rec-pi 1) 2 7)
" spent time "
(rtos (/ (- (car (_VL-TIMES)) time) 1000.) 2 3)
" sec."
)
)
(princ)
)
use Monte Carlo method for Pi
(defun monte-carlo-pi (n / i)
;; ElpanovEvgeniy
;; Calculation of Pi
;; Last edit: 10.02.2008
;; use Monte Carlo method for Pi
;|
(repeat 5 (princ (strcat "\n " (rtos (monte-carlo-pi 1000000.) 2 5)))(princ))
>>
3.14258
3.14138
3.14159
3.14105
3.14331
|;
(defun randnum (/ MODULUS RANDOM)
;|
http://intervision.hjem.wanadoo.dk/lisps/randnum.lsp
Randnum.lsp
Returns a random number.
Written by Paul Furman, 1996.
Based on algorithm by Doug Cooper, 1982.
(defun randnum (/ modulus multiplier increment random)
(if (not seed)
(setq seed (getvar "DATE"))
) ;_ if
(setq modulus 65536
multiplier 25173
increment 13849
seed (rem (+ (* multiplier seed) increment) modulus)
random (/ seed modulus)
) ;_ setq
) ;_ defun
(setq modulus 4294967296.0
multiplier 1664525
increment 1
)
Edit ElpanovEvgeniy
|;
(/ (setq seed (rem (+ (* 25173.
(if seed
seed
(setq seed (getvar "DATE"))
) ;_ if
) ;_ *
13849.
) ;_ +
65536
) ;_ rem
) ;_ setq
65536
) ;_ /
) ;_ defun
(setq i 0.
y (sqrt 0.5)
x (- 1. y)
) ;_ setq
(repeat (fix n)
(if (< (sqrt (+ (expt (+ y (* (randnum) x)) 2.) (expt (* (randnum) y) 2.))) 1.)
(setq i (1+ i))
) ;_ if
) ;_ repeat
(+ (* x y (/ i n) 8.) 2.)
)