Gamma function: Difference between revisions

(→‎Lanczos: Added.)
Line 4,731:
1.945403
2.709764</pre>
 
===Lanczos===
 
{{trans|Haskell}}
 
The Haskell version calculates the natural log of the gamma function, which is why the function is called <code>gammaln</code>; we correct that here by calling <code>exp</code>:
 
<syntaxhighlight lang="txrlisp">(defun gamma (x)
(let* ((cof #(76.18009172947146 -86.50532032941677
24.01409824083091 -1.231739572450155
0.001208650973866179 -0.000005395239384953))
(ser0 1.000000000190015)
(x55 (+ x 5.5))
(tmp (- x55 (* (+ x 0.5) (log x55))))
(ser (+ ser0 (sum [mapcar / cof (succ x)]))))
(exp (- (log (/ (* 2.5066282746310005 ser) x)) tmp))))
 
(each ((i (rlist 0.1..1.0..0.1 2..10)))
(put-line (pic "##.# ######.######" i (gamma i))))</syntaxhighlight>
 
{{out}}
 
<pre> 0.1 9.513508
0.2 4.590844
0.3 2.991569
0.4 2.218160
0.5 1.772454
0.6 1.489192
0.7 1.298055
0.8 1.164230
0.9 1.068629
1.0 1.000000
2.0 1.000000
3.0 2.000000
4.0 6.000000
5.0 24.000000
6.0 120.000000
7.0 720.000000
8.0 5040.000000
9.0 40320.000000
10.0 362880.000000
</pre>
 
=={{header|Visual FoxPro}}==
543

edits