Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate a factorial in C# using a library call?

Tags:

c#

factorial

I need to calculate the factorial of numbers up to around 100! in order to determine if a series of coin flip-style data is random, as per this Wikipedia entry on Bayesian probability. As you can see there, the necessary formula involves 3 factorial calculations (but, interestingly, two of those factorial calculations are calculated along the way to the third).

I saw this question here, but I'd think that integer is going to get blown out pretty quickly. I could also make a function that is more intelligent about the factorial calculation (ie, if I have 11!/(7!3!), as per the wiki example, I could go to (11*10*9*8)/3!), but that smacks of premature optimization to me, in the sense that I want it to work, but I don't care about speed (yet).

So what's a good C# library I can call to calculate the factorial in order to get that probability? I'm not interested in all the awesomeness that can go into factorial calculation, I just want the result in a way that I can manipulate it. There does not appear to be a factorial function in the Math namespace, hence the question.

like image 972
mmr Avatar asked Sep 30 '09 02:09

mmr


1 Answers

You could try Math.NET - I haven't used that library, but they do list Factorial and Logarithmic Factorial.

like image 76
TrueWill Avatar answered Sep 29 '22 01:09

TrueWill