Ejemplo n.º 1
0
        public static long Solve()
        {
            long          sum         = 0;
            List <string> pandigitals = EulerUtilities.Permute("0123456789");

            int[] primes = new int[] { 1, 2, 3, 5, 7, 11, 13, 17 };
            int   subNumber;
            bool  success;

            foreach (string pan in pandigitals)
            {
                if (pan[0].Equals("0"))
                {
                    continue;
                }
                success = true;
                for (int i = 0; i < pan.Length - 2; i++)
                {
                    subNumber = int.Parse(pan.Substring(i, 3));
                    if ((subNumber % primes[i]) != 0)
                    {
                        success = false;
                        break;
                    }
                }
                if (success)
                {
                    Console.WriteLine(pan);
                    sum += long.Parse(pan);
                }
            }
            return(sum);
        }
Ejemplo n.º 2
0
        public static int Solve()
        {
            int           min = 0, maxLength = 0;
            HashSet <int> badNumbers = new HashSet <int>();

            for (int start = 2; start <= 100000; start++)
            {
                List <int> chain   = new List <int>();
                List <int> factors = EulerUtilities.FindProperDivisors(start);
                int        current = factors.Sum();
                while (current > 1 && current <= 1000000)
                {
                    chain.Add(current);
                    factors = EulerUtilities.FindProperDivisors(current);
                    current = factors.Sum();
                    if (badNumbers.Contains(current))
                    {
                        break;
                    }
                    if (chain.Count > maxLength && chain.Contains(start))
                    {
                        maxLength = chain.Count;
                        min       = chain.Min();
                        break;
                    }
                    if (chain.Contains(current))
                    {
                        badNumbers.Add(start);
                        break;
                    }
                }
            }
            return(min);
        }
Ejemplo n.º 3
0
        //b has to be prime and positive for n^2 + a*n + b to give a positive prime for n=0
        public static int Solve()
        {
            long[] bValues = EulerUtilities.GeneratePrimes(1000).ToArray();
            int    maxStreak = 0;
            int    aResult = 0, bResult = 0;

            for (int a = -999; a < 0; a += 2)
            {
                for (int b = bValues.Length - 1; b >= 0; b--)
                {
                    int n = 0, streak = 0, num;
                    num = Math.Abs(n * n + a * n + (int)bValues[b]);
                    while (EulerUtilities.IsPrime(num))
                    {
                        n++;
                        streak++;
                        num = Math.Abs(n * n + a * n + (int)bValues[b]);
                    }

                    if (streak > maxStreak)
                    {
                        aResult   = a;
                        bResult   = (int)bValues[b];
                        maxStreak = streak;
                    }
                }
            }
            Console.WriteLine(aResult + ", " + bResult);
            return(aResult * bResult);
        }
Ejemplo n.º 4
0
        public static string Solve()
        {
            List <long> primes = EulerUtilities.GeneratePrimes(10000);

            primes.RemoveAll(x => x < 1000);
            HashSet <string> permutationSet;

            foreach (long prime in primes)
            {
                permutationSet = new HashSet <string>(EulerUtilities.Permute(prime.ToString()));
                permutationSet.RemoveWhere(x => !primes.Contains(int.Parse(x)));
                if (permutationSet.Count > 2 && !permutationSet.Contains("1487"))
                {
                    for (int diff = 1; diff < 5000; diff++)
                    {
                        string one = (prime + diff).ToString();
                        string two = (prime + 2 * diff).ToString();
                        if (permutationSet.Contains(one) && permutationSet.Contains(two))
                        {
                            return(prime + one + two);
                        }
                    }
                }
            }
            return("0");
        }
Ejemplo n.º 5
0
        public static int Solve()
        {
            List <int> primes = EulerUtilities.GeneratePrimes(1000000).ConvertAll(x => (int)x);

            int[] ways;
            int[] tempPrimes;
            for (int i = 10; i < Int32.MaxValue; i++)
            {
                tempPrimes = primes.Where(x => x <= i).ToArray();
                ways       = new int[i + 1];
                ways[0]    = 1;
                for (int prime = 0; prime < tempPrimes.Length; prime++)
                {
                    for (int way = tempPrimes[prime]; way <= i; way++)
                    {
                        ways[way] += ways[way - tempPrimes[prime]];
                    }
                }
                if (ways[i] > 5000)
                {
                    return(i);
                }
            }
            return(0);
        }
Ejemplo n.º 6
0
        public static long Solve()
        {
            List <long> pentas = new List <long>();

            pentas.Add(1);
            for (long i = 2; i < 10000; i++)
            {
                pentas.Add(i * (3 * i - 1) / 2);
            }
            long difference    = 0;
            long minDifference = long.MaxValue;

            for (int low = 0; low < pentas.Count; low++)
            {
                for (int high = low; high < pentas.Count; high++)
                {
                    difference = pentas[high] - pentas[low];
                    if (EulerUtilities.IsPentagonalNumber(pentas[low] + pentas[high]) && EulerUtilities.IsPentagonalNumber(pentas[high] - pentas[low]) && difference < minDifference)
                    {
                        minDifference = difference;
                    }
                }
            }
            return(minDifference);
        }
Ejemplo n.º 7
0
        public static int Solve()
        {
            int    count = 0;
            string text  = File.ReadAllText(@"..\..\txt\Problem042Text.txt");

            string[] words = text.Split(',');
            for (int i = 0; i < words.Length; i++)
            {
                words[i] = words[i].Replace("\"", "");
            }

            int wordSum;

            foreach (string word in words)
            {
                wordSum = 0;
                foreach (char c in word)
                {
                    wordSum += ((int)c) - 64;
                }
                if (EulerUtilities.IsTriangleNumber(wordSum))
                {
                    count++;
                }
            }
            return(count);
        }
Ejemplo n.º 8
0
        public static int Solve()
        {
            int        finalSum         = 0;
            List <int> positiveIntegers = new List <int>(Enumerable.Range(1, 28123));
            List <int> abundantIntegers = new List <int>();

            for (int i = 2; i < 28123; i++)
            {
                if (EulerUtilities.FindProperDivisors(i).Sum() > i)
                {
                    abundantIntegers.Add(i);
                }
            }

            int sum;

            for (int outer = 0; outer < abundantIntegers.Count; outer++)
            {
                for (int inner = outer; inner < abundantIntegers.Count; inner++)
                {
                    sum = abundantIntegers[outer] + abundantIntegers[inner];
                    if (sum > 28123)
                    {
                        break;
                    }
                    else
                    {
                        positiveIntegers.Remove(sum);
                    }
                }
            }
            finalSum = positiveIntegers.Sum();
            return(finalSum);
        }
Ejemplo n.º 9
0
        public static int Solve()
        {
            int  limit = 100000000;
            int  count = 0;
            bool mEven = true;

            for (int m = 2; m <= (int)((Math.Sqrt(1 + 2 * limit) - 1) / 2); m++)
            {
                int n = mEven ? 1 : 2;
                mEven = !mEven;
                int m2 = m * m;
                int mn = 2 * m * n;
                while (n < m && (2 * m2 + mn) <= limit)
                {
                    if (EulerUtilities.GreatestCommonDivisor(n, m) == 1)
                    {
                        int a = mn;
                        int b = m2 - n * n;
                        int c = m2 + n * n;
                        if (c % (b - a) == 0)
                        {
                            count += (limit / (a + b + c));
                        }
                    }
                    n += 2;
                    mn = 2 * m * n;
                }
            }
            return(count);
        }
Ejemplo n.º 10
0
        public static int Solve()
        {
            int limit = 1000000000;

            primes = EulerUtilities.GeneratePrimes(100).ConvertAll(x => (int)x);
            return(HammingCount(1, 0, limit));
        }
Ejemplo n.º 11
0
        public static int Solve()
        {
            int count = 0;

            for (int d = 2; d <= 12000; d++)
            {
                for (int n = 1; n < d; n++)
                {
                    if (n * 2 >= d)
                    {
                        break;
                    }
                    if (n * 3 <= d)
                    {
                        continue;
                    }
                    if (n % 2 == 0 && d % 2 == 0)
                    {
                        continue;
                    }
                    if (EulerUtilities.GreatestCommonDivisor(n, d) == 1)
                    {
                        count++;
                    }
                }
            }
            return(count);
        }
Ejemplo n.º 12
0
        public static int Solve()
        {
            List <long> primesLong = EulerUtilities.GeneratePrimes(1000000);
            List <int>  primes = primesLong.ConvertAll(x => (int)x);
            int         maxStreak = 1, maxPrime = 0;
            int         currentStreak, current;

            for (int start = 0; start < primes.Count; start++)
            {
                currentStreak = 1;
                current       = 0;
                for (int consec = start; consec < primes.Count; consec++)
                {
                    current += primes[consec];
                    if (current > 1000000)
                    {
                        break;
                    }
                    if (currentStreak > maxStreak && primes.Contains(current))
                    {
                        maxStreak = currentStreak;
                        maxPrime  = current;
                    }
                    currentStreak++;
                }
            }
            return(maxPrime);
        }
Ejemplo n.º 13
0
        public static int Solve()
        {
            int Lmax = 1500000;

            int[] counts = new int[Lmax + 1];
            for (int i = 2; i < Math.Sqrt(Lmax / 2); i++)
            {
                for (int j = 1; j < i; j++)
                {
                    if ((i + j) % 2 == 1 && EulerUtilities.GreatestCommonDivisor(i, j) == 1)
                    {
                        int a = (int)(Math.Pow(i, 2) - Math.Pow(j, 2));
                        int b = 2 * i * j;
                        int c = (int)(Math.Pow(i, 2) + Math.Pow(j, 2));
                        int L = a + b + c;
                        for (int k = 1; k *L < Lmax; k++)
                        {
                            counts[k * L] += 1;
                        }
                    }
                }
            }

            return(counts.Where(x => x == 1).Count());
        }
Ejemplo n.º 14
0
        public static int Solve()
        {
            EulerUtilities.LoadPrimes(int.MaxValue / 10);
            HashSet <long> uniques;
            int            streak = 0, answer = 0;

            for (int i = 646; i < int.MaxValue; i++)
            {
                uniques = EulerUtilities.UniquePrimeFactors((long)i);
                if (uniques.Count == 4)
                {
                    streak++;
                    if (streak == 4)
                    {
                        answer = i - 3;
                        break;
                    }
                }
                else
                {
                    streak = 0;
                }
            }
            return(answer);
        }
Ejemplo n.º 15
0
        public static int Solve()
        {
            int        limit  = 5000000;
            List <int> primes = EulerUtilities.GeneratePrimesSmall(2 * limit);
            int        count  = 0;

            for (long n = 2; n <= limit; n++)
            {
                long val = 2 * n * n - 1;

                int  bound   = (int)Math.Sqrt(val);
                int  i       = 1;
                int  prime   = primes[i];
                bool isPrime = true;
                while (prime <= bound)
                {
                    if (val % prime == 0)
                    {
                        isPrime = false;
                        break;
                    }
                    i++;
                    prime = primes[i];
                }
                if (isPrime)
                {
                    count++;
                }
            }
            return(count);
        }
Ejemplo n.º 16
0
        public static int Solve()
        {
            int    max = 0, count, maxPerim = 0;
            double c, perimeter;

            for (int perim = 120; perim <= 1000; perim++)
            {
                count = 0;
                for (int a = 1; a <= perim / 2; a++)
                {
                    for (int b = 1; b <= perim / 2; b++)
                    {
                        c         = Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));
                        perimeter = a + b + c;
                        if (perimeter == perim && EulerUtilities.IsWholeNumber(perimeter))
                        {
                            count++;
                        }
                    }
                }
                if (count > max)
                {
                    max      = count;
                    maxPerim = perim;
                }
            }
            return(maxPerim);
        }
Ejemplo n.º 17
0
        public static long Solve()
        {
            double     goal   = 15499.0 / 94744.0;
            List <int> primes = EulerUtilities.GeneratePrimes(100).ConvertAll(x => (int)x);

            EulerUtilities.LoadPrimes(100000000);
            long res = 1;

            for (int i = 0; i < primes.Count; i++)
            {
                res *= primes[i];
                if ((double)EulerUtilities.TotientCount(res) / (res - 1) < goal)
                {
                    res /= primes[i];
                    break;
                }
            }
            for (int i = 1; ; i++)
            {
                long r = res * i;
                if ((double)EulerUtilities.TotientCount(r) / (r - 1) < goal)
                {
                    return(r);
                }
            }
            return(0);
        }
Ejemplo n.º 18
0
        public static int Solve()
        {
            int limit = (int)Math.Pow(10, 4);
            int sum   = 0;

            bool mEven = true;

            for (int m = 2; m <= limit; m++)
            {
                int n = mEven ? 1 : 2;
                mEven = !mEven;
                int m2 = m * m;
                int mn = 2 * m * n;
                while (n < m && (2 * m2 + mn) <= limit)
                {
                    if (EulerUtilities.GreatestCommonDivisor(n, m) == 1)
                    {
                        int a = mn;
                        int b = m2 - n * n;
                        int c = m2 + n * n;
                        if (c % (b - a) == 0)
                        {
                            sum += (limit / (a + b + c));
                        }
                    }
                    n += 2;
                    mn = 2 * m * n;
                }
            }
            return(sum);
        }
Ejemplo n.º 19
0
        public static long Solve()
        {
            List <long> primes = EulerUtilities.GeneratePrimes(100000);

            primes.RemoveAll(x => (x < 1000 && x > 10000));
            double minRatio  = double.MaxValue;
            long   minNumber = 0;

            for (int i = 0; i < primes.Count; i++)
            {
                for (int j = i + 1; j < primes.Count; j++)
                {
                    long   num     = (primes[i] * primes[j]);
                    long   totient = (primes[i] - 1) * (primes[j] - 1);
                    double ratio   = ((double)num) / totient;
                    if (num > 10000000)
                    {
                        break;
                    }
                    if (ratio < minRatio && EulerUtilities.IsPermutation(num.ToString(), totient.ToString()))
                    {
                        minRatio  = ratio;
                        minNumber = num;
                    }
                }
            }
            return(minNumber);
        }
Ejemplo n.º 20
0
        public static long Solve()
        {
            int limit = 1000000000;

            primes = EulerUtilities.GeneratePrimesSmall(limit);
            HashSet <long> admissibles = GenerateAdmissible(limit);
            BitArray       primeBits   = new BitArray(limit, false);

            foreach (int p in primes)
            {
                primeBits[p] = true;
            }
            HashSet <int> uniques = new HashSet <int>();

            foreach (long admiss in admissibles)
            {
                for (int i = 2; i < int.MaxValue; i++)
                {
                    if (primeBits[(int)admiss + i])
                    {
                        uniques.Add(i);
                        break;
                    }
                }
            }
            return(uniques.Sum());
        }
Ejemplo n.º 21
0
        public static int Solve()
        {
            List <int> successfulStarts = new List <int>();
            List <int> chain            = new List <int>();

            for (int i = 3; i < 1000000; i++)
            {
                chain.Add(i);
                int current = (int)i.ToString().Sum(x => EulerUtilities.Factorial(x - 48));
                while (!chain.Contains(current))
                {
                    chain.Add(current);
                    current = (int)current.ToString().Sum(x => EulerUtilities.Factorial(x - 48));
                    if (chain.Count == 60 && chain.Contains(current))
                    {
                        successfulStarts.Add(i);
                    }
                    if (chain.Count > 60)
                    {
                        break;
                    }
                }
                chain.Clear();
            }
            return(successfulStarts.Count);
        }
Ejemplo n.º 22
0
        public static long Solve()
        {
            int limit = 1000000;

            EulerUtilities.LoadPrimes(limit);
            bool[] isPrime = new bool[limit + 1];
            long   sum     = 0;

            foreach (long p in EulerUtilities.Primes)
            {
                isPrime[p] = true;
            }
            int m, factorial;

            for (int i = 2; i <= limit; i++)
            {
                if (isPrime[i])
                {
                    sum += i;
                    Console.WriteLine(i);
                }
                else
                {
                    m         = 2;
                    factorial = 1;
                    while (factorial != 0)
                    {
                        factorial = (factorial * m) % i;
                        m++;
                    }
                    sum += m - 1;
                }
            }
            return(sum);
        }
Ejemplo n.º 23
0
        public static int Solve()
        {
            int        count = 0, lychrelCount;
            BigInteger number, reverse, result;

            for (int n = 10; n < 10000; n++)
            {
                lychrelCount = 0;
                number       = n;
                reverse      = BigInteger.Parse(String.Concat(number.ToString().Reverse()));
                result       = number + reverse;
                while (!EulerUtilities.IsPalindrome(result.ToString()) && lychrelCount < 50)
                {
                    number  = result;
                    reverse = BigInteger.Parse(String.Concat(number.ToString().Reverse()));
                    result  = number + reverse;
                    lychrelCount++;
                }
                if (lychrelCount == 50)
                {
                    count++;
                }
            }
            return(count);
        }
Ejemplo n.º 24
0
        public static int Solve()
        {
            int  compMinusTwoSquare = 0, square = 1;
            int  answer = 0;
            bool found = false;

            for (int composite = 35; composite < int.MaxValue && !found; composite += 2)
            {
                if (EulerUtilities.IsPrime(composite))
                {
                    continue;
                }

                square = 1;
                do
                {
                    compMinusTwoSquare = composite - 2 * (int)Math.Pow(square, 2);
                    square++;
                    if (EulerUtilities.IsPrime(compMinusTwoSquare))
                    {
                        break;
                    }
                } while(compMinusTwoSquare > 0);

                if (compMinusTwoSquare < 0)
                {
                    found  = true;
                    answer = composite;
                }
            }
            return(answer);
        }
Ejemplo n.º 25
0
        public static long Solve()
        {
            int         limit  = 10000000;
            long        sum    = 0;
            List <long> primes = EulerUtilities.GeneratePrimes(limit);

            for (int p = 0; p < primes.Count; p++)
            {
                for (int q = p + 1; q < primes.Count; q++)
                {
                    long pMul    = primes[p];
                    long largest = 0;
                    if (primes[p] * primes[q] > limit)
                    {
                        break;
                    }
                    while (pMul * primes[q] <= limit)
                    {
                        long inter = pMul * primes[q];
                        while (inter <= limit)
                        {
                            if (inter > largest)
                            {
                                largest = inter;
                            }
                            inter *= primes[q];
                        }
                        pMul *= primes[p];
                    }
                    sum += largest;
                }
            }
            return(sum);
        }
Ejemplo n.º 26
0
 private static void DrawCard(int[] deck, out int value)
 {
     value = deck[0];
     for (int i = 0; i < deck.Length - 1; i++)
     {
         EulerUtilities.Swap(deck, i, i + 1);
     }
 }
Ejemplo n.º 27
0
        public static Dictionary <int, Dictionary <int, int> > cache(int n)
        {
            var d = new Dictionary <int, Dictionary <int, int> >();

            for (int i = 1; i <= n; i++)
            {
                d.Add(i, EulerUtilities.PrimeFactorsWithCount(i));
            }
            return(d);
        }
Ejemplo n.º 28
0
        public static long Solve()
        {
            long gcd = 13;

            for (long n = 5; n <= Math.Pow(10, 3); n++)
            {
                Console.WriteLine((n - 1) + ": " + gcd + " | " + (double)gcd / (n - 1));
                gcd += EulerUtilities.GreatestCommonDivisorBinary(gcd, n);
            }
            return(gcd);
        }
Ejemplo n.º 29
0
        public static long Solve()
        {
            List <long> primes = EulerUtilities.GeneratePrimes(2000000);
            long        sum    = 0;

            foreach (long prime in primes)
            {
                sum += prime;
            }

            return(sum);
        }
Ejemplo n.º 30
0
        public static int Solve(int low, int high)
        {
            int squareSums = (int)Math.Pow(EulerUtilities.SumRange(low, high), 2);
            int sumSquares = 0;

            for (int i = low; i <= high; i++)
            {
                sumSquares += (int)Math.Pow(i, 2);
            }

            return(squareSums - sumSquares);
        }