Novahq.net Forum

Novahq.net Forum (https://novahq.net/forum/index.php)
-   Web design and Programming (https://novahq.net/forum/forumdisplay.php?f=32)
-   -   C#: Factorials, Combinations, and Permutations Class (https://novahq.net/forum/showthread.php?t=45341)

--BulletMagnet-- 09-03-2010 12:19 PM

C#: Factorials, Combinations, and Permutations Class
 
Code:

    // Class Name: Basic Counting Theory
    // Author: Bullet Magnet
    // Version: 1.0
    // Copyright: 2010 Bullet Magnet

    public class BasicCountingTheory
    {
        #region CaclulateFactorial
        // Factorial method
        // Accepts one parameter of type long
        public static long CalculateFactorial(long x)
        {
            long fact = 1;
            long i = 1; // Lowest possible term in a factorial

            // Loop through each subsequent terms of x
            while (i <= x)
            {
                fact = fact * i;
                i++;
            }
            return fact;
        }
        #endregion

        #region Permutation
        // This method calculates permutations, in which the order of a selection matters
        // Accepts 2 parameters for use in the main formula
        public static long Permutation(long a, long b) 
        {
            // Variable used to store result
            long result;
            // Main formula: (n!/((n-r)!))
            result = ((CalculateFactorial(a))/(CalculateFactorial(a-b)));

            return result;
        }
        #endregion

        #region Combination(long a, long b)
        // This method calclulates combinations, in which order of a selection doesn't matter
        public static long Combination(long a, long b)
        {
            // Variable used to store result
            long result;
            // Main formula: (n!/((n-r)!)r!)
            result = ((CalculateFactorial(a)) / ((CalculateFactorial(a - b) * CalculateFactorial(b))));

            return result;
        }
        #endregion
    }


ShadowZ 09-10-2010 09:43 AM

Permutations bring back nightmares... lol

--BulletMagnet-- 09-13-2010 12:10 AM

LoL, I enjoyed them at first, but now that we're working with them in probability, I'm not too fond of them.

ShadowZ 09-13-2010 11:17 PM

Yup, Probability gets very confusing if you try to skim over it... Lol


All times are GMT -5. The time now is 07:39 AM.

Powered by vBulletin®