Here is some javascript code that implements the Lanczos approximation to the gamma function (http://en.wikipedia.org/wiki/Lanczos_approximation\):
<script type="text/javascript">
// Gamma approximation; coefficients used by the GNU Scientific Library
function gamma(z) {
var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7];
if z < 0.5 {
return Math.PI / (Math.sin(Math.PI*z) * gamma(1-z));
} else {
z -= 1;
x = p[0];
for (var i = 0; i < p.length; i++) x += p/(z+i);
t = z + p.length - 1.5;
return Math.sqrt(2*Math.PI) * Math.pow(t, z+0.5) * Math.exp(-t) * x;
}
}
for (var x = 0.1; x < 3; x += 0.1) document.write("x: " + x + " gamma(x): " + gamma(x) + "<br/>");
</script>
This returns an empty page, which usually means there is a syntactical error in the code. In the absence of a proper debugger, can anyone point out where this error might be?
Thank you in advance.