/// <summary>
        /// A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4.
        /// If all of the permutations are listed numerically or alphabetically, we call it lexicographic order.
        /// The lexicographic permutations of 0, 1 and 2 are:
        /// 012   021   102   120   201   210
        /// What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
        /// </summary>
        /// <returns>Zero</returns>
        public string Compute()
        {
            string result           = null;
            string startingSequence = "0123456789";
            long   max          = 10;
            long   numberToFind = 1000000;

            for (long digit = 0; digit < max - 1; digit++)
            {
                long factorial = MathLibrary.Factorial(max - digit - 1);
                long position  = numberToFind / factorial;
                long remainder = numberToFind % factorial;

                if (remainder > 0)
                {
                    position++;
                }

                result           = result + startingSequence.Substring((int)position - 1, 1);
                startingSequence = startingSequence.Remove((int)position - 1, 1);

                long factorialBorder = (position - 1) * factorial;
                numberToFind = numberToFind - factorialBorder;
            }

            return(result + startingSequence);
        }
예제 #2
0
        public string Compute()
        {
            long[] digitFactorials = new long[10];
            long   result          = 0;

            for (int digit = 0; digit < 10; digit++)
            {
                digitFactorials[digit] = MathLibrary.Factorial(digit);
            }

            for (int number = 3; number < 50000; number++)
            {
                List <long> digits       = MathLibrary.GetDigits(number);
                long        factorialSum = 0;
                foreach (long digit in digits)
                {
                    factorialSum += digitFactorials[digit];
                }
                if (factorialSum == number)
                {
                    result += number;
                }
            }

            return(result.ToString());
        }
예제 #3
0
    public void Factorial()
    {
        var expected = BigInteger.Parse("1551118753287382280224243016469303211063259720016986112000000000000");
        var actual   = MathLibrary.Factorial(51);

        Assert.Equal(expected, actual);
    }
예제 #4
0
    /// <summary>
    /// Compute the number of permutations nPk.
    /// </summary>
    /// <param name="k"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public static BigInteger PermutationCount(int n, int k)
    {
        if (n < 1)
        {
            throw new ArgumentOutOfRangeException(nameof(n));
        }

        if (k < 0 || k > n)
        {
            throw new ArgumentOutOfRangeException(nameof(k));
        }

        return(MathLibrary.Factorial(n) / MathLibrary.Factorial(n - k));
    }
예제 #5
0
    /// <summary>
    /// Compute the number of distinct permutations when duplicate values are present
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static BigInteger PermutationCountDistinct(string str)
    {
        var digitCounts = str
                          .GroupBy(i => i)
                          .Select(kvp => kvp.Count());

        var num = MathLibrary.Factorial(str.Length);
        var den = BigInteger.One;

        foreach (var count in digitCounts)
        {
            den *= MathLibrary.Factorial(count);
        }

        return(num / den);
    }
예제 #6
0
 public static BigInteger CircularPermutationCount(long n) =>
 MathLibrary.Factorial(n - 1);
예제 #7
0
 public static BigInteger AnagramCount(IEnumerable <int> m) =>
 MathLibrary.Factorial(m.Aggregate((a, v) => a + v)) / m.Aggregate(BigInteger.One, (a, v) => a * MathLibrary.Factorial(v));