コード例 #1
0
        /*
         *
         * https://projecteuler.net/problem=5
         *
         * 2520 is the smallest number that can be divided by each of the numbers
         * from 1 to 10 without any remainder.
         *
         * What is the smallest positive number that is evenly divisible by all of
         * the numbers from 1 to 20?
         */

        public static int FindSmallestNumberOfMultiplesUpTo(int limit)
        {
            var dict  = new Dictionary <long, int>();
            var range = new EulerRange(2, limit);

            foreach (var item in range)
            {
                var primes = Helper.FindPrimeMultiples(item);
                foreach (var prime in primes)
                {
                    var cnt = primes.Count(x => x == prime);
                    if (!dict.ContainsKey(prime))
                    {
                        dict.Add(prime, 1);
                    }
                    if (dict[prime] < cnt)
                    {
                        dict[prime] = cnt;
                    }
                }
            }
            var result = 1;

            foreach (var item in dict)
            {
                result *= (int)Math.Pow(item.Key, item.Value);
            }
            return(result);
        }
コード例 #2
0
        private int OptimizationAttempt1(int numDigits)
        {
            if (numDigits == 1)
            {
                return(9);
            }

            var upperLimit = (int)Math.Pow(10, numDigits) - 1;
            var lowerLimit = upperLimit - (int)Math.Pow(10, numDigits - 1);

            var           range   = new EulerRange(lowerLimit, upperLimit);
            HashSet <int> numbers = new HashSet <int>();

            //Get every multiple
            foreach (var item in range)
            {
                range.Select(x => numbers.Add(item * x)).ToList();
            }

            foreach (var item in numbers.OrderByDescending(x => x))
            {
                if (NumberStringHelper.IsNumberPalindrome(item))
                {
                    return(item);
                }
            }
            return(-1);
        }
コード例 #3
0
        private static void Taks1()
        {
            Console.WriteLine("Taks1:");
            IRange fibonacci = new FibonacciRange(10);

            Console.Write("Fibonacci:");
            fibonacci.Iterate(x => Console.Write("  {0}", x));
            Console.WriteLine();

            IRange euler = new EulerRange(10);

            Console.Write("Euler:");
            euler.Iterate(x => Console.Write("  {0}", x));
            Console.WriteLine();
        }
コード例 #4
0
        private static void Task3()
        {
            Console.WriteLine("Taks3:");
            IRange fibonacci  = new FibonacciRange(10);
            IRange euler      = new EulerRange(10);
            IRange rangeSumma = new RangeSumma(10, fibonacci, euler);

            Console.Write("Fibonacci + Euler:");
            rangeSumma.Iterate(x => Console.Write("  {0}", x));
            Console.WriteLine();
            IRange rangeDifference = new RangeDifference(10, fibonacci, euler);

            Console.Write("Fibonacci - Euler:");
            rangeDifference.Iterate(x => Console.Write("  {0}", x));
            Console.WriteLine();
        }
コード例 #5
0
        private static void Task2()
        {
            Console.WriteLine("Taks2:");
            IRange fibonacci = new FibonacciRange(10);
            IRange negativeFibonacciRange = new NegativeRange(fibonacci);

            Console.Write("Negative Fibonacci:");
            negativeFibonacciRange.Iterate(x => Console.Write("  {0}", x));
            Console.WriteLine();
            IRange euler = new EulerRange(10);
            IRange negativeEulerRange = new NegativeRange(euler);

            Console.Write("Negative Euler:");
            negativeEulerRange.Iterate(x => Console.Write("  {0}", x));
            Console.WriteLine();
        }
コード例 #6
0
        private int BruteForce(int numDigits)
        {
            var range   = new EulerRange((int)Math.Pow(10, numDigits - 1), (int)Math.Pow(10, numDigits) - 1);
            var numbers = new List <(int left, int right, int total)>();

            //Get every multiple
            foreach (var item in range)
            {
                numbers.AddRange(range.Select(x => (x, item, item * x)));
            }

            foreach (var item in numbers.OrderByDescending(x => x.total))
            {
                if (NumberStringHelper.IsNumberPalindrome(item.total))
                {
                    return(item.total);
                }
            }

            return(-1);
        }