To add to blue_cowdawg's advice, if you need to roll your own, what you are seeking is the
probability density function of the chi-squared distribution, which is given
here where x is the chi-square value and k is the number of degrees of freedom, and you will need to include values from the
Gamma function, which has a closed form solution for half-integer value (i.e. k/2).