C#: Basic Combinatorics

Basic combinatorics in C# is a quite straight forward task if we only talk about relatively small numbers like (100!). For bigger number you will need Arbitrary-precision arithmetic.
This is the final class:

static class BasicCombinatorics {
        /// <summary>
        /// One object in each position,
        /// With elementary counting,
        /// With repetitions,
        /// With grouping of same objects
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="ni">Groups of same objects</param>
        /// <returns>Number of permutations with groups of same objects</returns>
        public static double PermutationsSimilarGroups( int n, List<int> ni ) {

            var p = 1.0;
            foreach (var i in ni) {
                p *= Factorial( i );
            }

            return p / Factorial( n );
        }

        /// <summary>
        /// One object in each position,
        /// With elementary counting,
        /// With repetitions,
        /// No groups of same objects
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="k">Number of positions</param>
        /// <returns>Number of provisions with repetition of n per k</returns>
        public static double ProvisionsReps( int n, int k ) {
            return Math.Pow( n, k );
        }

        /// <summary>
        /// One object in each position,
        /// With elementary counting,
        /// No repetitions
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="k">Number of positions</param>
        /// <returns>Number of simple provisions</returns>
        public static double SimpleProvisions( int n, int k ) {
            return Factorial( n ) / Factorial( n - k );
        }

        /// <summary>
        /// One object in each position,
        /// No elementary counting,
        /// With repetitions
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="k">Number of positions</param>
        /// <returns>Number of combinations</returns>
        public static double CombinationsΡeps( int n, int k ) {
            return Factorial( n + k - 1 ) / (Factorial( k ) * Factorial( n - 1 ));
        }

        /// <summary>
        /// One object in each position,
        /// No elementary counting,
        /// No repetitions
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="k">Number of positions</param>
        /// <returns>Number of simple combinations</returns>
        public static double SimpleCombinations( int n, int k ) {
            return Factorial( n ) / (Factorial( k ) * Factorial( n - k ));
        }

        /// <summary>
        /// Many object in each position,
        /// With same objects
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="m">Number of positions</param>
        /// <returns>Number of combinations of same objects</returns>
        public static double SameObjectsInSlots( int n, int m ) {
            return Factorial( m + n - 1 ) / (Factorial( n ) * Factorial( m - 1 ));
        }

        /// <summary>
        /// Many object in each position,
        /// No same objects,
        /// With distinguished objects
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="m">Number of positions</param>
        /// <returns>Number of combinations of distinguished objects</returns>
        public static double DifObjectsInSlotsBySeries( int n, int m ) {
            return Factorial( m + n - 1 ) / Factorial( m - 1 );
        }

        /// <summary>
        /// Many object in each position,
        /// No same objects,
        /// No distinguished objects
        /// --Ν ΑΝΤΙΚΕΙΜΕΝΑ ΣΕ Μ ΥΠΟΔΟΧΕΣ
        /// </summary>
        /// <param name="n">Number of objects</param>
        /// <param name="m">Number of positions</param>
        /// <returns>Number of combinations</returns>
        public static double DifObjectsInSlots( int n, int m ) {
            return Math.Pow( m, n );
        }

        /// <summary>
        /// Factorial of a number
        /// </summary>
        /// <param name="n">n!</param>
        /// <returns>The factoriar of a number</returns>
        public static double Factorial( int n ) {
            double result = 1;
            for (int i = n; i >= 1; i--)
                result *= i;
            return result;
        }

        /// <summary>
        /// Sum of a number
        /// </summary>
        /// <param name="n">Σn</param>
        /// <returns>The sum of a number</returns>
        public static double Sum( int n ) {
            double result = 0;
            for (int i = n; i >= 1; i--)
                result += i;
            return result;
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s