Example #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Running!");
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            int        largest = 1;                       // Index referencing largest (i.e. first) value found
            int        digits  = 9;                       // Number of digits in the sequence
            List <int> panSeq  = new List <int> (digits); // The pandigital sequence

            for (int n = 1; n <= digits; n++)             // Populate based on number of digits
            {
                panSeq.Add(n);
            }
            Permutations permutations = new Permutations(panSeq);        // Instantiate object

            permutations.GetPermutations();                              // Generate the permutations

            for (int i = permutations.Count - 1; i >= 0; i--)            // For each permutation, starting at the end
            {
                List <int> permutation = permutations.GetPermutation(i); // Grab it from the object
                if (!TestOne(permutation) &&               // Test all the configurations
                    !TestTwo(permutation) &&
                    !TestThree(permutation) &&
                    !TestFour(permutation))
                {
                    continue;            // Didn't pass any; move onto the next
                }
                else                     // One of them passed
                {
                    largest = i;         // Set the index that passed
                    break;
                }
            }

            // Grab the permutation using the index and turn it into an integer for printout
            List <int>    largestPermutation = permutations.GetPermutation(largest);
            StringBuilder largestString      = new StringBuilder();

            foreach (int digit in largestPermutation)
            {
                largestString.Append(digit);
            }
            int largestVal = Int32.Parse(largestString.ToString());

            stopWatch.Stop();
            Console.WriteLine("{0} found in {1} milliseconds", largestVal,
                              stopWatch.ElapsedMilliseconds);
        }
Example #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Running!");
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            int        largest = 1;
            int        digits  = 9;
            List <int> panSeq  = new List <int> (digits);

            for (int n = 1; n <= digits; n++)
            {
                panSeq.Add(n);
            }
            Permutations permutations = new Permutations(panSeq);

            permutations.GetPermutations();

            for (int i = permutations.Count - 1; i >= 0; i--)
            {
                List <int> permutation = permutations.GetPermutation(i);
                if (!TestOne(permutation) &&
                    !TestTwo(permutation) &&
                    !TestThree(permutation) &&
                    !TestFour(permutation))
                {
                    continue;
                }
                else
                {
                    largest = i;
                    break;
                }
            }

            List <int>    largestPermutation = permutations.GetPermutation(largest);
            StringBuilder largestString      = new StringBuilder();

            foreach (int digit in largestPermutation)
            {
                largestString.Append(digit);
            }
            int largestVal = Int32.Parse(largestString.ToString());

            stopWatch.Stop();
            Console.WriteLine("{0} found in {1} milliseconds", largestVal,
                              stopWatch.ElapsedMilliseconds);
        }
        public void TestGetNextPermutation3Chars()
        {
            Permutations permu = new Permutations(new List <char>()
            {
                '2', '1', '0'
            });

            String expected = "012";

            Assert.AreEqual(expected, permu.GetPermutation(0));

            expected = "021";
            Assert.AreEqual(expected, permu.GetPermutation(1));

            expected = "102";
            Assert.AreEqual(expected, permu.GetPermutation(2));
        }
        public void TestGetNextPermutation4Chars()
        {
            Permutations permu = new Permutations(new List <char>()
            {
                '2', '1', '0', '3'
            });

            String expected = "0123";

            Assert.AreEqual(expected, permu.GetPermutation(0));

            expected = "0132";
            Assert.AreEqual(expected, permu.GetPermutation(1));

            expected = "0213";
            Assert.AreEqual(expected, permu.GetPermutation(2));

            expected = "0231";
            Assert.AreEqual(expected, permu.GetPermutation(3));

            expected = "0312";
            Assert.AreEqual(expected, permu.GetPermutation(4));

            expected = "0321";
            Assert.AreEqual(expected, permu.GetPermutation(5));

            expected = "1023";
            Assert.AreEqual(expected, permu.GetPermutation(6));
        }
        public void TestGetNextPermutation5Chars()
        {
            Permutations permu = new Permutations(new List <char>()
            {
                '2', '1', '0', '3', '4'
            });


            String expected = "01234";

            Assert.AreEqual(expected, permu.GetPermutation(0));

            expected = "01243";
            Assert.AreEqual(expected, permu.GetPermutation(1));

            expected = "01324";
            Assert.AreEqual(expected, permu.GetPermutation(2));

            expected = "01342";
            Assert.AreEqual(expected, permu.GetPermutation(3));

            expected = "01423";
            Assert.AreEqual(expected, permu.GetPermutation(4));

            expected = "01432";
            Assert.AreEqual(expected, permu.GetPermutation(5));
        }
Example #6
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Running!");
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            // Get all the pandigital permutations of nine digits
            int          digits       = 9;
            Permutations permutations = new Permutations(digits);

            permutations.GetPermutations();

            // Generate the primes up to 17
            List <int> primes = new List <int> ();

            primes.Add(2);
            Boolean isPrime;

            for (int p = 3; p <= 17; p++)
            {
                isPrime = true;
                for (int i = 0; primes [i] * primes [i] <= p; i++)
                {
                    if (p % primes [i] == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                {
                    primes.Add(p);
                }
            }

            List <int> permutation = new List <int> ();
            int        offset;
            int        value;
            bool       isDivisible;
            double     sum = 0;
            double     permutationValue;
            double     multiple;

            for (int i = 0; i < permutations.Count; i++)
            {
                permutation = permutations.GetPermutation(i);
                isDivisible = true;
                // Quick test for divisibility by 2 and 5
                if (permutation [3] % 2 != 0)
                {
                    continue;
                }
                if (permutation [5] != 5 && permutation [5] != 0)
                {
                    continue;
                }
                for (int p = 6; p >= 0; p--)
                {
                    offset = 6 - p;
                    value  = permutation [9 - offset] +
                             (10 * permutation [8 - offset]) +
                             (100 * permutation [7 - offset]);
                    if (value % primes [p] == 0)
                    {
                        continue;
                    }
                    else
                    {
                        isDivisible = false;
                        break;
                    }
                }
                if (isDivisible)
                {
                    permutationValue = 0;
                    for (int o = 1; o <= 10; o++)
                    {
                        multiple          = (int)Math.Pow(10, o - 1);
                        permutationValue += permutation [10 - o] * multiple;
                    }
                    sum += permutationValue;
                }
            }

            stopWatch.Stop();
            Console.WriteLine("{0} found in {1} milliseconds", sum,
                              stopWatch.ElapsedMilliseconds);
        }