X

Combinatorics Exercises

The combinatorics goes very well with the program, who better than a computer, in fact, can help us to make even the most complex calculations on the odds? In this article I will explain how to solve some exercises in combinatorics, what is interesting is to see how you can do it all in a computer programming language, in this case it is the C#. I chose C# because it is easy and especially close to both the C/C++ and Java, but the same procedure can be easily adapted to any other development language.
First we need to create a class that I call “CalcComb” in which the constructor takes two numeric arguments, the number of items and the number of groups, the classical (n, k) of combinatorics. The first function of the class must necessarily be that of calculating the factorial of a number, then that is equivalent to the simple permutations of that number of objects. Here is the source code of the class:

class CalcComb
{
   int m_n = 0;
   int m_k = 0;

   public CalcComb(int n, int k)
   {
      if (n > 20) n = 20;
      if (k > 20) k = 20;

      m_n = n;
      m_k = k;
    }

    public long Factorial(bool bN = true)
    {
      long r = 1;
      int j = bN ? m_n : m_k;

      for (int i = j; i > 0; i--)
         r *= i;

      return r;
     }
   }

   class Program
   {
      static void Main(string[] args)
      {
        CalcComb c = new CalcComb(5, 3);

        System.Console.WriteLine("Simple permutations of n
             are: " + c.Factorial());
        System.Console.WriteLine("Simple permutations of k
             are: " + c.Factorial(false) + "\n");

        System.Console.ReadLine();
    }
}

Once you have the initial values in the constructor of the class I created a function to calculate the factorial of value n and value k, everything depends upon the function of type boolean. In the “main” function of program I created an example with the press of the values of two factorials at the end I asked for a value to be input to stop the flow of the program, useful for performing the same, without debugging from within the development environment.
To calculate other values such provisions or combinations need to change the function for calculating the factorial of a number by making it more general and not only specific attributes of the class members.

class CalcComb
{
   int m_n = 0;
   int m_k = 0;

   public CalcComb(int n, int k)
   {
      if (n > 20) n = 20;
      if (k > 20) k = 20;

      m_n = n;
      m_k = k;
   }

   public long Factorial(int n)
   {
       long r = 1;

       if (n > 20)
          n = 20;

       for (int i = n; i > 0; i--)
          r *= i;

       return r;
    }

    public long Provisions(bool bRepeat = false)
    {
       if (!bRepeat)
       {
          long ftn = Factorial(m_n);
          long ftk = Factorial(m_n - m_k);

          return ftn / ftk;
       }

       return (long)Math.Pow(m_n, m_k);
    }

    public long Combinations()
    {
       long ftn = Factorial(m_n);
       long fth = Factorial(m_k);
       long ftk = Factorial(m_n - m_k);

       return ftn / (fth * ftk);
    }
 }

 class Program
 {
    static void Main(string[] args)
    {
        CalcComb c = new CalcComb(5, 3);

        System.Console.WriteLine("Simple permutations (5):
             " + c.Factorial(5));
        System.Console.WriteLine("Simple provisions (5,3):
             " + c.Provisions());
        Syste.Console.WriteLine("Provisions with repetition (5,3)
             " + c.Provisions(true));
        System.Console.WriteLine("Simple combinations (5,3):
             " + c.Combinations());

        System.Console.ReadLine();
    }
}

The following code above is only a way of example, only serves to show how it is possible to easily implement simple algorithms for solving exercises related to the combinatorics.

giampy107:
X

Cookies Policy

We use cookies to personalize content and ads, provide social media features and analyze our traffic. We also share information about your use of our site with our web analytics, advertising and social media partners who may combine it with other information you have provided to them or that they have collected based on your use of theirs. services.

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings