Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
0
        /// <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());
        }