Holly Macaroni! What a show!
In any case, the correct way to compute such things with large intermediate elements is log () them
p = exp(log(p)) log(p) = log(365!) - n*log(365) - log((365 - n)!)
For the factorial, use the Gamma function, G (n + 1) = n !, and the C-library has a very convenient function that calculates log (G (x)): lgamma (x)
More loops, long doubles, bignum libraries, overflows ...
code
#include <math.h> #include <stdio.h> double b(int n) { double l = lgamma(365.0 + 1.0) - (double)n * log(365.0) - lgamma(365.0 - (double)n + 1.0); return exp(l); } int main() { double p = b(20); printf("%e %e\n", p, 1.0 - p); return 0; }
source share