Exemple #1
0
 /// <summary>
 /// Get permutations of a list of chars
 /// </summary>
 /// <param name="list"></param>
 public static List<string> GetPermutations(char[] list)
 {
     var rawPermutations = new Permutations<char>(list);
     var permutations = rawPermutations.Select(chars => string.Join("", chars));
     
     return permutations.ToList();
 }
Exemple #2
0
        public override void Run()
        {
            var set = "123456789";
            var len = set.Length;

            int max = 0;

            while (len > 0)
            {
                var matches = new List <int>();

                var perm = new Permutations <char>(set.Take(len).ToList());
                foreach (IEnumerable <char> digits in perm)
                {
                    var n = Convert.ToInt32(string.Join("", digits));
                    if (math.isPrime(n))
                    {
                        max = Math.Max(n, max);
                    }
                }

                --len;
            }

            Console.WriteLine(max);
        }
Exemple #3
0
        public static IEnumerable <IList <int> > Permutations(this IEnumerable <int> set)
        {
            var perm = new Permutations(set);

            while (perm.GetNextPerm())
            {
                yield return(perm.Set);
            }
        }
        public static HashSet <long> Generate(int nDigit)
        {
            var numbers      = Enumerable.Range(1, nDigit);
            var numberString = string.Empty;

            foreach (var number in numbers)
            {
                numberString += number;
            }
            return(Permutations.Generate(long.Parse(numberString)));
        }
Exemple #5
0
        public override void Run()
        {
            var digits = "0123456789";
            var perm   = new Permutations <char>(digits.ToList());
            var values = perm
                         .Select(p => Convert.ToInt64(string.Join("", p)))
                         .OrderBy(i => i)
                         .ToArray();

            Console.WriteLine(values[1000000 - 1]);
        }
 public void SetData()
 {
     char[] chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
     Permutations<Char> perms = new Permutations<Char> (chars);
     foreach (IList<Char> p in perms)
     {
         StringBuilder b = new StringBuilder ();
         b.AppendFormat ("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8],
         p[9]);
         data.Add (b.ToString ());
     }
 }
        public void Permutations_SetWith3Elements_5PermutationsShoudBeReturned()
        {
            var perm = new Permutations(new[] { 1, 2, 3 });
            var res  = new List <string>();

            while (perm.GetNextPerm())
            {
                res.Add(perm.Set.Select(i => i.ToString(CultureInfo.InvariantCulture)).Aggregate((a, b) => a + b));
            }

            Assert.IsTrue(res.Count == 5);
            Assert.IsTrue(res.Contains("132"));
            Assert.IsTrue(res.Contains("213"));
            Assert.IsTrue(res.Contains("231"));
            Assert.IsTrue(res.Contains("312"));
            Assert.IsTrue(res.Contains("321"));
        }
Exemple #8
0
        public override long Solve()
        {
            long circularPrimeCount = 0;
            var  primesLookup       = Primes.CalculatePrimesBelow(max);

            foreach (var prime in primesLookup)
            {
                if (PossibleCircularPrime(prime))
                {
                    var rotations = Permutations.GenerateNumberRotations(prime);
                    if (rotations.All(p => primesLookup.Contains(p)))
                    {
                        circularPrimeCount++;
                    }
                }
            }
            return(circularPrimeCount);
        }
        public static HashSet <long> GenerateZeroToN(int nDigit)
        {
            var numbers = Enumerable.Range(1, nDigit).ToList();

            //append 0 at end so cast to long doesn't chop it off
            numbers.Add(0);

            var numberString = string.Empty;

            foreach (var number in numbers)
            {
                numberString += number;
            }
            var pandigitals = Permutations.Generate(long.Parse(numberString));

            //remove any permutations that begin with 0, gets removed in the cast
            //and therefore isn't a 0 - n pandigital.
            pandigitals.RemoveWhere(p => p.ToString().Length != nDigit + 1);
            return(pandigitals);
        }
Exemple #10
0
        public override long Solve()
        {
            for (int number = 1490; number < 9999; number++)
            {
                var perms     = Permutations.Generate(number).Where(p => p.ToString().Length == 4);
                var permsList = new List <long>();

                perms.ToList().ForEach(p => { if (Primes.IsPrime(p))
                                              {
                                                  permsList.Add(p);
                                              }
                                       });
                if (permsList.Count >= 4)
                {
                    var sequence = FindSequence(permsList);
                    if (sequence.Count > 0 && !sequence.Contains(1487))
                    {
                        return(long.Parse(sequence[0].ToString() + sequence[1].ToString() + sequence[2].ToString()));
                    }
                }
            }
            throw new Exception("Cannot find sequence....you messed it up");
        }
Exemple #11
0
        public override void Run()
        {
            // create a running total
            ulong sum = 0;

            // create an array to hold the mods
            var mods = new[] { 2, 3, 5, 7, 11, 13, 17 };

            // create a set of the largest 0-9 pandigital number
            var set = "9876543210".ToList();

            // permutate the number to get all of the 0-9 pandigital numbers
            // that don't begin with '0'
            var perm = new Permutations <char>(set);

            // go through the numbers
            foreach (IList <char> digits in perm)
            {
                var allHoldProperty = true;
                var i = 3;
                var m = 0;
                while (allHoldProperty && i < digits.Count)
                {
                    var text = string.Concat(digits[i - 2], digits[i - 1], digits[i]);
                    var num  = Convert.ToInt32(text);
                    allHoldProperty &= (num % mods[m++]) == 0;
                    ++i;
                }

                if (allHoldProperty)
                {
                    sum += Convert.ToUInt64(string.Concat(digits));
                }
            }

            Console.WriteLine(sum);
        }
Exemple #12
0
        /// <summary>
        /// Get a specific permutation
        /// </summary>
        /// <param name="list"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetPermutation(char[] list, int index)
        {
            var rawPermutations = new Permutations<char>(list);
            var permutations = rawPermutations.Select(chars => string.Join("", chars)).ToList();

            if (permutations.Count <= index)
            {
                throw new ArgumentException("Index is beyond the number of permutations");
            }

            return permutations[index];
        }
Exemple #13
0
        public override void Run()
        {
            var allDigits = "123456789".ToList();
            var table     = new HashSet <int>();

            // a is the number of digits in the multiplicand.
            for (var a = 1; a < allDigits.Count; ++a)
            {
                // create a combinations object to choose the digits for the multiplicand
                var multiplicandDigitChooser = new Combinations <char>(allDigits, a);
                foreach (var multiplicandDigits in multiplicandDigitChooser)
                {
                    // get the available digits for the multiplier
                    var availableMultiplierDigits = allDigits.Except(multiplicandDigits).ToList();

                    // now that we have the multiplicand digits, we need to generate permutations of the digits
                    var multiplicandNumbers =
                        new Permutations <char>(multiplicandDigits)
                        .Select(ch => Convert.ToInt32(string.Join("", ch)))
                        .ToArray();

                    // b is the number of digits in the multiplier.
                    for (var b = 1; b < availableMultiplierDigits.Count; ++b)
                    {
                        // create a combinations object to choose the digits for the multiplier
                        var multiplierDigitChooser = new Combinations <char>(availableMultiplierDigits, b);
                        foreach (var multiplierDigits in multiplierDigitChooser)
                        {
                            // now that we have the multiplier digits, we need to generate permutations of the digits
                            var multiplierNumbers =
                                new Permutations <char>(multiplierDigits)
                                .Select(ch => Convert.ToInt32(string.Join("", ch)))
                                .ToArray();

                            // get the available digits for the product, and sort them
                            var availableProductDigits = string.Join("", availableMultiplierDigits
                                                                     .Except(multiplierDigits)
                                                                     .OrderBy(c => c));

                            // now, we need to multiply the two together and see if they match the available
                            // product digits
                            for (var i = 0; i < multiplicandNumbers.Length; ++i)
                            {
                                var multiplicand = multiplicandNumbers[i];
                                for (var j = 0; j < multiplierNumbers.Length; ++j)
                                {
                                    var multiplier = multiplierNumbers[j];
                                    var product    = multiplicand * multiplier;
                                    var text       = string.Join("", product.ToString().OrderBy(c => c));
                                    if (text.Equals(availableProductDigits))
                                    {
                                        // the multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital,
                                        // so add the product to the table
                                        table.Add(product);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // return the sum of the values in the table
            Console.WriteLine(table.Sum());
        }