# Binomial coefficient C#

What is called a binomial Coefficient in two ways-a Symbol of the Newton?

Sometimes a situation where we need to determine the number of combinations of the elements of the collection. This is a situation that, contrary to appearances, it happens very often, but not always, there is a need to make calculations, and decisions are made intuitively.

For example, you can consider several situations where such choice is made:

-Released a series of 4 collectable cards of famous athletes, and we can afford only 2 of them. With this set you can choose the card numbers {1,2}, {1,3}, {1.4}, {2,3}, {2,4}, {3,4}, and so we can make 6 different choices.

-Favourite number game draws 5 non-repeating numbers from 60 available. The number of combinations is much greater than in the previous example, and print all it would take a lot of space in this article.

-The company employs 100 workers, and half of them will receive earlier this month. How many combinations of employees can check here to occur? Many employers do not reflects on this and doing your own breakdown.

Menu wpisu

# The Definition Of The Binomial Coefficient

What if, however, we would like to find out how much we really choice and what are they? Then with the formula defined by Newton, called Binomial or binomial coefficient. This is a function of two arguments which are nonnegative integers. The solution is Newton’s Symbol how many possible combinations of your choice can be obtained by selecting the k elements from a set of n-element set. The definition of the model is as follows:

# The result of Newton’s Symbol in the code

The code presented represents the solution to the above formula, without the use of recursion for the strong. It is applied in the relationship that is created at the time of shortening parts counter with more the product of denominator. Clarification on a particular example should dispel doubts. Counter in Newton’s Symbol is always going to be greater than the denominator. Selecting n = 8 and k = 5 you get a fraction:

Reducing the counter (8!) with a higher factor of the denominator (5!) we get:

-in the numerator 6 * 7 * 8 (because 1 * 2 * 3 * 4 * 5 will be shortened),

-to use in the denominator of 1 * 3! (so 1 * 2 * 3).

In the following code, the number 1 is multiplied by another counter values (6 * 7 * 8) and divided by the following elements of the denominator (1 * 2 * 3). This relationship simplifies entry and working code for one condition and one loop.

1 2 3 4 5 6 7 8 9 10 11 12 |
long WyliczIloscGrup (long n, long k)//zwraca the number of combinations { If (k > n-k) k = n-k; long liczGrupy = 1; for (int i = 0; i + k; and < +) { liczGrupy = liczGrupy * (n-i); liczGrupy = liczGrupy/(i + 1); } return liczGrupy; } |

# Group code combinations

Sometimes information about the number of possible combinations is not sufficient and we would like to find out what items they make up. The code is relatively simple in the case when it is the number of elements that has be drawn and how much is available. The situation becomes more complicated when the function is supposed to be universal and useful for any values.

The following is the code for the function that gets the previously presented the parameters n and k, and returns an array of arrays. Returns an array of typed array stored in each row with the values of belonging to the group. The rows so you can interpret it as just another of the group, and their columns as drawn in them.

The algorithm requires clarification, but frogs better understand it also follows the example of n = 6, k = 4.

Define drawn values is done in the while loop. Her first call stores originally defined group (1, 2, 3, .., k-in our example 1, 2, 3, 4). Subsequent calls to the loop increases the last item in the original array, up to the maximum value on it equal to n (i.e.-> 1, 2, 3, 5-> 1, 2, 3, 6). When this value, the variable is decremented by 1, which creates a reference to an element with a lower index (at this stage, it is the value of the 3 position equal to 3, which is changed to 4). Elements with larger indices from it are incremented by 1 in respect of a new variable (1,2 remain unchanged, 4 amended a moment ago and subsequent elements 4 + 1 = 5, which gives another group of 1, 2, 4, 5), and the variable p is again a value of k so that the reference to the last element.

Referencing elements with more and smaller indexes is carried out when the last item assumes the maximum value more than once, for the consecutive elements (eg. for the first time for a group of 1, 2, 4, 6 where p is reduced to 3, the second time 1, 2, 5, 6 where p is reduced to 2). The last group of reduce variable to 0 and thanks and we counter that this is the last of the possible combinations.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
Determination of groups of k-tuples from a set of n-element int [] [] WyznaczGrupy (int n, int k) { int i = 0, p = 0, count = 0; the number of possible groups of numbers-the number of rows in the array long iloscGrup = WyliczIloscGrup (n, k); k-ary array declaration for individual groups int [] pojedynczaGrupa = new in[k]t; the Declaration of an array of arrays for all groups int [] [] wszystkieGrupy = new int[iloscGrup] []; for each row is assigned an empty array is one-dimensional, k-elementowa for (i = 0; i iloscGrup; i ++ <) wszystkieGrupy[i] = new int[k]; to complement the array of k-ary values from 1 to k for (i = 1; i = i + k; < +) pojedynczaGrupa[i - 1] = i; and finally, less numbers than we have available (n) If (k < n) p = k; all n-count shall be drawn else statement p = 1; Count-the number of the current augmented group While (counter < iloscGrup) { When the last item in the group is equal to the latest available value-> 's last case If (pojedynczaGrupa[k - 1] == n) p is the position of the originally defined k-ary array to which the jump to increase its value by one, and according to it, set the following items. p-; else statement p = k; If (p > 0) exceeds the scope of the defined groups, and (count! = 0) this is not the first group If ((p > 0) & & (count! = 0)) { for (i = k; and > = p; i--) pojedynczaGru[i - 1]pa = pojedynczaGr[p - 1]upa +-p + 1; } the assignment created a single group to the corresponding row in the set of all groups for (int j = 0; j < k; j ++)//completion next row wszystkieGrupy[licznik][j] = pojedynczaGrupa[j]; counter ++; } return wszystkieGrupy; } |

# View all combinations

The following code was introduced in the collector a string variable data on the composition of the groups and their numbering in a manner pleasing to the human eye:

1-1 2 3 4 5

2-1 2 3 4 6

3-1 2 3 4 7

4-1 2 3 4 8

….

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
View int line = 0; count = 0; While (counter < iloscGrup) { line ++; wyswietlwszystkieGrupy += "n"; wyswietlwszystkieGrupy += line. ToString (); wyswietlwszystkieGrupy += ""; for (int j = 0; j < k; j ++)//completion next row wyswietlwszystkieGrupy + = wszystk[licznik][j]ieGrupy () + ""; counter ++; } |