diff --git a/src/funktion.c b/src/funktion.c index 6b9bc45..ace92d4 100644 --- a/src/funktion.c +++ b/src/funktion.c @@ -253,3 +253,15 @@ double probability_from_tree(double successful_outcomes, double total_outcomes, result = successful_outcomes / (total_outcomes * branches); return result; } +int binomial_coefficient(int n, int k) { + + int result = 1; + if (k > n - k) { + k = n - k; + } + for (int i = 0; i < k; ++i) { + result = result * (n - i) / (i + 1); + } + printf("The binomial coefficient C(%d, %d) is %d\n", n, k, result); + return result; +} diff --git a/src/funktion.h b/src/funktion.h index b2c9872..1125b81 100644 --- a/src/funktion.h +++ b/src/funktion.h @@ -42,5 +42,6 @@ float Vskalort(float x1, float x2, float x3, float z1, float z2, float z3); double Vangle(float x1, float x2, float x3, float z1, float z2, float z3); float vPunkt(float x1, float x2, float x3, float z1, float z2, float z3, float p1, float p2, float p3); double probability_from_tree(double successful_outcomes, double total_outcomes, double branches); +int binomial_coefficient(int n, int k); #endif diff --git a/src/main.c b/src/main.c index 3bbf8ba..0e58328 100644 --- a/src/main.c +++ b/src/main.c @@ -200,4 +200,9 @@ int main() y = getValue('T'); z = getValue('B'); probability_from_tree(x, y, z); + + printf("Enter a and b to calculate the binomial coefficient: "); + a = getValue('a'); + b = getValue('b') + binomial_coefficient(a, b); }