Esempio n. 1
0
 /// <summary>
 /// Adds all combinations of decimals.
 /// </summary>
 /// <param name="list"></param>
 /// <param name="intermediateResults"></param>
 public static void AddAllDecimals(List <string> list, IntermediateResults intermediateResults)
 {
     for (int x = 0; x < list.Count; x++)
     {
         var newList = new List <string>(list);
         var digit   = list[x];
         if (!digit.Contains("."))
         {
             int start = 0;
             if (digit.StartsWith("-"))
             {
                 start = 1;
             }
             for (int i = start; i < digit.Length; i++)
             {
                 var newDigit = $"{digit.Substring(0, i)}.{digit.Substring(i, digit.Length - i)}";
                 newList[x] = newDigit;
                 if (intermediateResults.Add(new Digits(newList)))
                 {
                     AddAllDecimals(newList, intermediateResults);
                 }
             }
         }
     }
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            DateTime startTime           = DateTime.Now;
            var      intermediateResults = new IntermediateResults();


            var unaryOperators = new List <UnaryOperator>()
            {
                new UnaryOperator("({0})!", (arg) => Factorial(arg), (arg) => FactorialIsValid(arg)),
                new UnaryOperator("({0})!!", (arg) => DoubleFactorial(arg), (arg) => FactorialIsValid(arg)),
                //new UnaryOperator("({0})!!!", (arg) => TrippleFactorial(arg), (arg) => FactorialIsValid(arg)),
                new UnaryOperator("sqrt({0})", (arg) => Math.Sqrt(arg), (arg) => arg >= 0 ? true: false),
                //new UnaryOperator("abs({0})", (arg) => Math.Abs(arg), (arg) => arg>=0 ? true: false)
            };

            var binaryOperators = new List <BinaryOperator>()
            {
                new BinaryOperator("({0} + {1})", (arg1, arg2) => arg1 + arg2),
                new BinaryOperator("({0} - {1})", (arg1, arg2) => arg1 - arg2),
                new BinaryOperator("({0} * {1})", (arg1, arg2) => arg1 * arg2),
                new BinaryOperator("({0} / {1})", (arg1, arg2) => arg1 / arg2),
                new BinaryOperator("({0} ^ {1})", (arg1, arg2) => Math.Pow(arg1, arg2)),
                // This doesn't work because there isn't a simple modulus operator in the Math library. And Mod isn't allowed in this solution.
                //new BinaryOperator("({0}%{1})", (arg1, arg2) => Mod(arg1,arg2)),
            };

            var start = new List <string>()
            {
                "2", "0", "1", "7"
            };

            // Generate all the Permutations
            foreach (var nums in GetPermutations(start))
            {
                //Console.WriteLine(string.Join(" ", nums));
                intermediateResults.Add(new Digits(nums));
                AddAllDecimals(nums.ToList(), intermediateResults);

                // Combine numbers
                var results = CombineOne(nums.ToList());

                foreach (var result in results)
                {
                    intermediateResults.Add(new Digits(result));

                    // Handle the decimal point
                    AddAllDecimals(result, intermediateResults);

                    // Get combinations
                    //foreach (var combo in GetAllCombos(result))
                    //{
                    //    intermediateResults.Add(new Digits(combo));
                    //}
                }
            }

            //Handle Negatives
            //foreach (var digits in intermediateResults.Values.ToArray() )
            //{
            //    AddAllNegatives(digits.OriginalDigits, intermediateResults);
            //}


            //foreach (var result in intermediateResults)
            //{
            //    Console.WriteLine($"{result.Key} ");
            //}



            bool gotResults = false;

            do
            {
                var thisIterationList = intermediateResults.Values.Where(f => !f.IsProcessed).ToArray();
                //var thisIterationList = intermediateResults.Values.ToArray();
                gotResults = false;
                // Handle unary  sqrt, factorial, etc.
                foreach (Digits digits in thisIterationList)
                {
                    foreach (UnaryOperator unaryOperator in unaryOperators)
                    {
                        for (int i = 0; i < digits.Count; i++)
                        {
                            gotResults |= intermediateResults.Add(digits, unaryOperator, i);
                        }
                    }
                }

                // Handle binary  +, -, *, etc.
                foreach (Digits digits in thisIterationList)
                {
                    foreach (BinaryOperator binaryOperator in binaryOperators)
                    {
                        for (int i = 1; i < digits.Count; i++)
                        {
                            gotResults |= intermediateResults.Add(digits, binaryOperator, i);
                        }
                    }
                    digits.IsProcessed = true;
                }
                // Loop until we don't find any new digit combinations.
            } while (gotResults);

            // Get a list of all numbers found.
            var finalResult = new List <Digits>();

            foreach (var result in intermediateResults)
            {
                //Console.WriteLine($"{result.Key} ");
                if (result.Value.Count == 1)
                {
                    finalResult.Add(result.Value);
                }
            }

            int count = 0;

            for (double i = 1; i <= 100; i++)
            {
                var digit = finalResult.FirstOrDefault(f => f[0] == i);
                if (digit != null)
                {
                    count++;
                    var value      = digit[0];
                    var expression = digit.DigitCalculations[0];
                    Console.WriteLine($"{ i } = { expression }");
                }
                else
                {
                    Console.WriteLine($"{ i } = ?");
                }
            }

            Console.WriteLine($"Total between 1 and 100: {count}");
            Console.WriteLine($"Total found: {finalResult.Count}");
            Console.WriteLine($"Total Time: {(DateTime.Now - startTime).TotalMilliseconds}ms");
            Console.ReadLine();
        }
Esempio n. 3
0
        public static Dictionary <double, string> Solve(int seed = 2017)
        {
            var start = new List <string>();

            foreach (var num in seed.ToString())
            {
                start.Add(int.Parse(num.ToString()).ToString());
            }
            var intermediateResults = new IntermediateResults();


            var unaryOperators = new List <UnaryOperator>()
            {
                new UnaryOperator("({0})!", (arg) => Factorial(arg), (arg) => FactorialIsValid(arg)),
                new UnaryOperator("({0})!!", (arg) => DoubleFactorial(arg), (arg) => FactorialIsValid(arg)),
                //new UnaryOperator("({0})!!!", (arg) => TrippleFactorial(arg), (arg) => FactorialIsValid(arg)),
                new UnaryOperator("sqrt({0})", (arg) => Math.Sqrt(arg), (arg) => arg >= 0 ? true: false),
                //new UnaryOperator("abs({0})", (arg) => Math.Abs(arg), (arg) => arg>=0 ? true: false)
            };

            var binaryOperators = new List <BinaryOperator>()
            {
                new BinaryOperator("({0}+{1})", (arg1, arg2) => arg1 + arg2),
                new BinaryOperator("({0}-{1})", (arg1, arg2) => arg1 - arg2),
                new BinaryOperator("({0}*{1})", (arg1, arg2) => arg1 * arg2),
                new BinaryOperator("({0}/{1})", (arg1, arg2) => arg1 / arg2),
                new BinaryOperator("({0}^{1})", (arg1, arg2) => Math.Pow(arg1, arg2)),
                // This doesn't work because there isn't a simple modulus operator in the Math library. And Mod isn't allowed in this solution.
                //new BinaryOperator("({0}%{1})", (arg1, arg2) => Mod(arg1,arg2)),
            };


            // Generate all the Permutations
            foreach (var nums in GetPermutations(start))
            {
                //Console.WriteLine(string.Join(" ", nums));
                intermediateResults.Add(new Digits(nums));
                AddAllDecimals(nums.ToList(), intermediateResults);

                // Combine numbers
                var results = CombineOne(nums.ToList());

                foreach (var resultValue in results)
                {
                    intermediateResults.Add(new Digits(resultValue));

                    // Handle the decimal point
                    AddAllDecimals(resultValue, intermediateResults);

                    // Get combinations
                    //foreach (var combo in GetAllCombos(result))
                    //{
                    //    intermediateResults.Add(new Digits(combo));
                    //}
                }
            }

            //Handle Negatives
            //foreach (var digits in intermediateResults.Values.ToArray() )
            //{
            //    AddAllNegatives(digits.OriginalDigits, intermediateResults);
            //}


            //foreach (var result in intermediateResults)
            //{
            //    Console.WriteLine($"{result.Key} ");
            //}



            bool gotResults = false;

            do
            {
                gotResults = false;
                // Handle unary  sqrt, factorial, etc.
                foreach (Digits digits in intermediateResults.Values.ToArray())
                {
                    foreach (UnaryOperator unaryOperator in unaryOperators)
                    {
                        for (int i = 0; i < digits.Count; i++)
                        {
                            gotResults |= intermediateResults.Add(digits, unaryOperator, i);
                        }
                    }
                }

                // Handle binary  +, -, *, etc.
                foreach (Digits digits in intermediateResults.Values.ToArray())
                {
                    foreach (BinaryOperator binaryOperator in binaryOperators)
                    {
                        for (int i = 1; i < digits.Count; i++)
                        {
                            gotResults |= intermediateResults.Add(digits, binaryOperator, i);
                        }
                    }
                }
                // Loop until we don't find any new digit combinations.
            } while (gotResults);


            var finalResult = new List <Digits>();

            foreach (var resultValue in intermediateResults)
            {
                //Console.WriteLine($"{result.Key} ");
                if (resultValue.Value.Count == 1)
                {
                    finalResult.Add(resultValue.Value);
                }
            }

            var result = new Dictionary <double, string>();

            foreach (var digit in finalResult.OrderBy(f => f[0]))
            {
                if (digit[0] > 0 && digit[0] <= 100)
                {
                    result.Add(digit[0], digit.DigitCalculations[0]);
                }
            }
            return(result);
        }