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()); }
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()); }