コード例 #1
0
        public static string Problem3()
        {
            Console.WriteLine("\n\t3) What is the largest prime factor of the number 600,851,475,143?");

            int diviser = 2;

            // This number will be divided several times until it cannot be divided by numbers other than 1 and itself,
            // which means we have found the biggest prime factor of the initial value.
            long numberToBeDivided = 600851475143;

            int initialValueSqrt = (int)Math.Sqrt(numberToBeDivided);// A prime factor of a number cannot be larger that the number's square root.

            for (int i = 0; i < initialValueSqrt; i++)
            {
                // The numberToBeDivided is divided by the same diviser for as long as the number is divisible by that specific diviser.
                while (numberToBeDivided % diviser == 0)
                {
                    numberToBeDivided /= diviser;
                }

                if (MathyHelper.IsPrime(numberToBeDivided))
                {
                    break;
                }

                // If the numberToBeDivided is not divisable by the current diviser anymore but it is not a prime number, we move onto the next diviser.
                diviser++;
            }

            return(numberToBeDivided.ToString());
        }
コード例 #2
0
        public static string Problem5()
        {
            Console.WriteLine("\n\t5) What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?");

            for (int i = 20; i < int.MaxValue; i += 20)
            {
                if (MathyHelper.IsDivisibleFrom1To20(i))
                {
                    return(i.ToString());
                }
            }

            return("Unable to find the answer.");
        }
コード例 #3
0
        public static string Problem7()
        {
            Console.WriteLine("\n\t7) What is the 10,001st prime number?");

            long number  = 3;
            int  counter = 1; // 2 is already counted.

            while (counter != 10001)
            {
                if (MathyHelper.IsPrime(number))
                {
                    counter++;
                }

                number += 2;
            }

            return((number - 2).ToString());
        }
コード例 #4
0
        public static string Problem4()
        {
            Console.WriteLine("\n\t4) Find the largest palindrome made from the product of two 3 digit numbers.");

            int currentProduct;
            int greatestProduct = 0;

            // These loops iterate through every single possible combination of 3-digit numbers and multiplays them.
            for (int i = 999; i > 99; i--)
            {
                // Second loop always starts with whatever the value of i is since we have already calculated all the multiplication combinations of the previous i.
                for (int j = i; j > 99; j--)
                {
                    currentProduct = i * j;

                    if (MathyHelper.IsPalendromic(currentProduct.ToString()) && currentProduct > greatestProduct)
                    {
                        greatestProduct = currentProduct;
                    }
                }
            }

            return(greatestProduct.ToString());
        }