/// <summary> /// Finds sum of all primes below n /// </summary> /// <param name="n"></param> /// <returns></returns> public static long Problem10(long n) { long Sum = 0; for (int i = 1; i <= n; i++) { if (MathsFunctions.IsPrime(i)) { Sum += i; } } long Correction = Sum - 1; return(Correction); }
/// <summary> /// Finds the largest prime divisor of n /// </summary> /// <param name="n">works for values of i*i < n</param> /// <returns>returns largest value</returns> public static long Problem3(long n = 600851475143) { long div = 1; for (long i = 1; i *i <= n; i++) { if (n % i == 0) { if (MathsFunctions.IsPrime(i)) { div = i; } } } return(div); }
/// <summary> /// Finds the nth prime number by iteratively adding primes to a list, then returns the last value on the list /// </summary> /// <param name="n">nth prime</param> /// <returns></returns> public static long Problem7(int n) { List <long> PrimeList = new List <long>(); for (int i = 2; i < n * n; i++) { if (MathsFunctions.IsPrime(i)) { PrimeList.Add(i); long[] Primes = PrimeList.ToArray(); if (Primes.Length == n) { break; } } } return(PrimeList.Last()); }
/// <summary> /// Finds the largest palindrome for 2 n digits values multiplied together /// </summary> /// <param name="n"></param> /// <returns></returns> public static int Problem4(int n = 3) { int Largest = 0; for (int i = 10 ^ (n - 1); i < Math.Pow(10, n); i++) { for (int j = 10 ^ (n - 1); j < Math.Pow(10, n); j++) { int t = i * j; if (MathsFunctions.IsPalindrome(t)) { if (t > Largest) { Largest = t; } } } } return(Largest); }