3
\brief Contains some probability functions
10
** combinations() : Calculates the number of ways to choose k objects
11
** from n objects, or "n choose k"
14
** \param n = number of objects in total
15
** \param k = number of objects taken at a time
18
** number of combinations of n objects taken k at a time ("n choose k")
19
** (returned as a double).
23
double combinations(int n, int k)
25
double factorial(int) ;
28
if (n == k) return (1.0) ;
29
else if (k > n) return(0.0) ;
30
else if (k == 0) return(1.0) ;
31
comb = factorial(n) / (factorial(k) * factorial(n-k)) ;
38
** factorial(): Returns n!
41
** \param n = number to take factorial of
44
** n factorial, as a double word (since n! can get very large).
47
double factorial(int n)
50
if (n == 0 || n == 1) return(1.0);
51
if (n < 0) return(0.0) ;
53
return ((double) n * factorial(n-1)) ;
60
** test combinations routines
68
double combinations() ;
70
printf("Enter two numbers: ") ;
71
scanf("%d %d", &i, &j) ;
72
printf("i! = %.2f\n", factorial(i)) ;
73
printf("j! = %.2f\n", factorial(j)) ;
74
printf("i choose j = %.2f\n", combinations(i,j)) ;