public static long Solution()
        {
            var  stopwatch  = new Stopwatch();
            long solution   = 1;
            var  exponents  = new List <int>();
            var  primes     = UtilityFunctions.Primes(7376512);
            var  candidates = new List <long> {
                1
            };

            for (int i = 0; i < primes.Count; i++)
            {
                var primePower   = primes[i] * primes[i];
                var maxCandidate = candidates.Max();

                if (primePower > primes.Last() && primePower > maxCandidate)
                {
                    break;
                }
                while (primePower < Math.Max(primes.Last(), maxCandidate))
                {
                    candidates.Add(primePower);
                    if (maxCandidate > primes.Last())
                    {
                        candidates.Remove(maxCandidate);
                        maxCandidate = candidates.Max();
                    }
                    else
                    {
                        primes.RemoveAt(primes.Count - 1);
                    }
                    primePower *= primePower;
                }
            }

            foreach (var t in candidates.Concat(primes))
            {
                solution = (solution * t) % 500500507;
            }

            Console.WriteLine($"Time spent finding max {stopwatch.ElapsedMilliseconds}");
            return(solution);
        }
Exemple #2
0
        public static long Solution()
        {
            long solution = 0;
            var  primes   = UtilityFunctions.Primes(limit / 2);

            for (int i = 0; i < primes.Count; i++)
            {
                var         p         = primes[i];
                List <long> powersOfP = new List <long>();
                var         n         = p;
                while (n < limit / p)
                {
                    powersOfP.Add(n);
                    n *= p;
                }
                for (int j = i + 1; j < primes.Count; j++)
                {
                    var q = primes[j];
                    if (p * q > limit)
                    {
                        break;
                    }
                    var  powersOfPTimesQ = new List <long>(powersOfP);
                    long max             = 0;
                    while (true)
                    {
                        powersOfPTimesQ = powersOfPTimesQ.Where(t => t * q <= limit).Select(t => t * q).ToList();
                        if (powersOfPTimesQ.Count == 0)
                        {
                            break;
                        }
                        max = Math.Max(powersOfPTimesQ.Max(), max);
                    }
                    solution += max;
                }
            }



            return(solution);
        }
Exemple #3
0
        public static long Solution()
        {
            long solution = 0;
            long mLimit   = (long)1e4;

            HashSet <long> squares = new HashSet <long>();

            for (long m = 2; m < mLimit; m++)
            {
                long kLimit = (long)Math.Sqrt(limit / (m * m * m) + 1);
                var  step   = 2 - (m % 2);
                for (long r = 1; r < m; r += step)
                {
                    if (UtilityFunctions.Gcd(m, r) != 1)
                    {
                        continue;
                    }
                    if (m * m * m * r + r * r >= limit)
                    {
                        break;
                    }
                    for (long k = 1; k <= kLimit; k++)
                    {
                        long n = k * k * m * m * m * r + k * r * r;
                        if (n >= limit)
                        {
                            break;
                        }
                        if (UtilityFunctions.IsPerfectSquare(n))
                        {
                            if (!squares.Contains(n))
                            {
                                solution += n;
                                squares.Add(n);
                            }
                        }
                    }
                }
            }
            return(solution);
        }
Exemple #4
0
        public static long Solution()
        {
            long           solution = 0;
            HashSet <long> strongTruncatableHarshads;

            var rightTruncatableHarshads = RightTruncatableHarshads(limit, out strongTruncatableHarshads);

            foreach (var s in strongTruncatableHarshads)
            {
                foreach (var i in new[] { 1, 3, 7, 9 })
                {
                    var n = 10 * s + i;
                    if (UtilityFunctions.IsPrime(n))
                    {
                        solution += n;
                    }
                }
            }

            return(solution);
        }
Exemple #5
0
        public static long Solution()
        {
            long solution = 0;

            var primes = UtilityFunctions.Primes(limit + 100);

            for (int i = 3; true; i++)
            {
                var p1 = primes[i - 1];
                var p2 = primes[i];

                if (p1 > limit)
                {
                    break;
                }
                long powerOf10 = (long)Math.Pow(10, p1.ToString().Length);
                var  x         = (-p1 * UtilityFunctions.ModularInverse(powerOf10, p2) % p2 + p2) % p2;
                solution += powerOf10 * x + p1;
            }

            return(solution);
        }
        public static long Solution()
        {
            long solution = 0;

            for (int a = 1; a <= limit; a++)
            {
                for (int c = 1; c <= a; c++)
                {
                    for (int b = 1; b <= limit; b++)
                    {
                        for (int d = 1; d <= b; d++)
                        {
                            var internalPoints =
                                ((a + c) * (b + d)
                                 - UtilityFunctions.Gcd(a, b)
                                 - UtilityFunctions.Gcd(b, c)
                                 - UtilityFunctions.Gcd(c, d)
                                 - UtilityFunctions.Gcd(d, a)) / 2 + 1;
                            if (UtilityFunctions.IsPerfectSquare(internalPoints))
                            {
                                solution += 4;
                                if (a == c || b == d)
                                {
                                    solution -= 2;
                                    if (a == c && b == d)
                                    {
                                        solution -= 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }


            return(solution);
        }
        public static long Solution()
        {
            var  stopwatch = new System.Diagnostics.Stopwatch();
            long solution  = 0;

            var divisorsOfOrder = UtilityFunctions.Divisors(order);
            var primes          = UtilityFunctions.Primes((int)Math.Sqrt(UtilityFunctions.IntegralPower(2, order / 2) + 1));

            var primeFactors  = UtilityFunctions.PrimeFactors(UtilityFunctions.IntegralPower(2, order) - 1, primes);
            var reducedPrimes = primeFactors.Select(t => t.Item1).ToList();

            foreach (var d in divisorsOfOrder)
            {
                var m = UtilityFunctions.Moebius(d, null, primes);
                if (m != 0)
                {
                    var divisorsOfPowerOfTwo = UtilityFunctions.Divisors(UtilityFunctions.IntegralPower(2, order / d) - 1, null, reducedPrimes);
                    solution += (divisorsOfPowerOfTwo.Sum() + divisorsOfPowerOfTwo.Count()) * m;
                }
            }

            return(solution);
        }
Exemple #8
0
        public static long Solution()
        {
            long solution = 0;

            var primes = UtilityFunctions.Primes(limit);

            foreach (var p in primes)
            {
                var ord = UtilityFunctions.MultiplicativeOrder(10, 9 * p);
                if (ord == 0)
                {
                    solution += p;
                    continue;
                }
                while (ord % 2 == 0 || ord % 5 == 0)
                {
                    if (ord % 2 == 0)
                    {
                        ord /= 2;
                    }
                    if (ord % 5 == 0)
                    {
                        ord /= 5;
                    }
                }
                if (ord > 1)
                {
                    solution += p;
                }
                else
                {
                    Console.WriteLine(p);
                }
            }

            return(solution);
        }
        public static long Solution()
        {
            long solution  = 0;
            var  counter   = 0;
            var  stopwatch = new Stopwatch();

            stopwatch.Start();
            var previousTime = 0.0;
            var currentTime  = 0.0;

            var primes = UtilityFunctions.Primes(1000000);

            currentTime = stopwatch.ElapsedMilliseconds;
            Console.WriteLine($"Settig up took {currentTime - previousTime} milliseconds.");
            previousTime = currentTime;

            foreach (var p in primes)
            {
                var order = UtilityFunctions.MultiplicativeOrder(10, 9 * p);
                if (order == 0)
                {
                    continue;
                }
                if (limit % order == 0)
                {
                    solution += p;
                    counter  += 1;
                    Console.WriteLine(p);
                    if (counter >= 40)
                    {
                        return(solution);
                    }
                }
            }
            return(solution);
        }
        internal static long PandigitalPrimeSets(List <long> digits)
        {
            if (digits.Count == 0)
            {
                return(1);
            }
            long count = 0;

            digits.Sort();
            var maxElement   = digits[digits.Count - 1];
            var restOfDigits = UtilityFunctions.Complement(new List <long> {
                maxElement
            }, digits);
            var subsets = UtilityFunctions.GenerateSubsets(restOfDigits);

            foreach (var subset in subsets)
            {
                subset.Add(maxElement);
                var complement           = UtilityFunctions.Complement(subset, digits);
                var subsetDigitSignature = UtilityFunctions.DigitSignature(subset, ProblemDigits);
                count += PrimeCandidates[subsetDigitSignature].Count * PandigitalPrimeSets(complement);
            }
            return(count);
        }
        public static long Solution1()
        {
            long solution     = 0;
            long power        = UtilityFunctions.IntegralPower(2, order) - 1;
            var  primes       = UtilityFunctions.Primes(UtilityFunctions.IntegralPower(2, order / 4));
            var  primeFactors = new List <long>();
            var  ordersModP   = new List <long>();
            var  powersOfTwo  = new Dictionary <long, long>();
            bool keyAdded     = false;
            var  factors      = new Dictionary <long, long>();

            for (int i = 2; i <= Math.Sqrt(order); i++)
            {
                if (order % i == 0)
                {
                    powersOfTwo.Add(i, UtilityFunctions.IntegralPower(2, i) - 1);
                    if (i * i != order)
                    {
                        powersOfTwo.Add(order / i, UtilityFunctions.IntegralPower(2, order / i) - 1);
                    }
                }
            }

            var keys = powersOfTwo.Keys.OrderBy(t => t).ToArray();

            foreach (var p in primes)
            {
                if (power < p * p)
                {
                    keyAdded = false;
                    primeFactors.Add(power);
                    foreach (var key in keys)
                    {
                        if (powersOfTwo[key] % power == 0)
                        {
                            keyAdded = true;
                            ordersModP.Add(key);
                            break;
                        }
                    }
                    if (!keyAdded)
                    {
                        ordersModP.Add(order);
                    }
                    break;
                }
                var primePower = p;
                while (power % p == 0)
                {
                    keyAdded = false;
                    primeFactors.Add(p);
                    foreach (var key in keys)
                    {
                        if (powersOfTwo[key] % primePower == 0)
                        {
                            keyAdded = true;
                            ordersModP.Add(key);
                            break;
                        }
                    }
                    if (!keyAdded)
                    {
                        ordersModP.Add(order);
                    }
                    power      /= p;
                    primePower *= p;
                }
            }

            factors.Add(1, 1);

            for (int i = 0; i < primeFactors.Count; i++)
            {
                keys = factors.Keys.ToArray();
                foreach (var key in keys)
                {
                    var newKey = key * primeFactors[i];
                    if (!factors.ContainsKey(newKey))
                    {
                        factors.Add(newKey, UtilityFunctions.Lcm(factors[key], ordersModP[i]));
                    }
                }
            }

            foreach (var factor in factors)
            {
                if (factor.Value == order)
                {
                    solution += factor.Key + 1;
                }
            }


            return(solution);
        }
Exemple #12
0
        public static long SolutionOld()
        {
            long answer     = 0;
            var  validPairs = new Dictionary <long, HashSet <long> >();
            var  validSums  = new HashSet <long>();

            for (long p = 2; p < limit; p++)
            {
                for (long q = 1; q < p; q++)
                {
                    if (p + q > limit)
                    {
                        break;
                    }
                    if (p % 2 == 1)
                    {
                        if ((q % 2 == 1) && (p * q) % 4 != 3)
                        {
                            continue;
                        }
                        if (q % 4 == 2)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if ((p % 4 == 2) && (q % 2 == 1))
                        {
                            continue;
                        }
                    }

                    var aSquare = (p * p + q * q + p * q);
                    if (!UtilityFunctions.IsPerfectSquare(aSquare))
                    {
                        continue;
                    }
                    if (validPairs.ContainsKey(p))
                    {
                        validPairs[p].Add(q);
                        if (validPairs.ContainsKey(q))
                        {
                            long sum = long.MaxValue;
                            foreach (var r in validPairs[q])
                            {
                                if (validPairs[p].Contains(r))
                                {
                                    var bSquare = p * p + r * r + p * r;
                                    var cSquare = r * r + q * q + r * q;
                                    if (bSquare + cSquare < aSquare)
                                    {
                                        var bc = Math.Sqrt(bSquare * cSquare);
                                        if (bSquare + cSquare + bc < aSquare)
                                        {
                                            continue;
                                        }
                                    }
                                    sum = p + q + r;
                                    if (sum < limit)
                                    {
                                        validSums.Add(sum);
                                        Console.WriteLine($"{p} {q} {r} {sum}");
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        validPairs.Add(p, new HashSet <long> {
                            q
                        });
                    }
                }
            }
            foreach (var sum in validSums)
            {
                answer += sum;
            }
            return(answer);
        }
Exemple #13
0
        public static long Solution()
        {
            long answer     = 0;
            var  validPairs = new Dictionary <long, HashSet <long> >();
            var  validSums  = new HashSet <long>();

            for (long m = 2; m < limit; m++)
            {
                var stepSize = 2 - (m % 2);
                for (long n = 1; n < m; n += stepSize)
                {
                    if (UtilityFunctions.Gcd(m, n) != 1 || (m - n % 3) == 0)
                    {
                        continue;
                    }

                    var firstSide  = 2 * m * n + n * n;
                    var secondSide = m * m - n * n;
                    if (m * m + 2 * m * n > limit)
                    {
                        break;
                    }
                    var p = Math.Max(firstSide, secondSide);
                    var q = Math.Min(firstSide, secondSide);
                    for (long k = 1; k < limit; k++)
                    {
                        if (k * (p + q) > limit)
                        {
                            break;
                        }
                        if (validPairs.ContainsKey(k * p))
                        {
                            validPairs[k * p].Add(k * q);
                        }
                        else
                        {
                            validPairs.Add(k * p, new HashSet <long> {
                                k *q
                            });
                        }
                    }
                }
            }

            foreach (var p in validPairs.Keys)
            {
                foreach (var q in validPairs[p])
                {
                    if (validPairs.ContainsKey(q))
                    {
                        long sum = long.MaxValue;
                        foreach (var r in validPairs[q])
                        {
                            if (validPairs[p].Contains(r))
                            {
                                var aSquare = p * p + q * q + p * q;
                                var bSquare = p * p + r * r + p * r;
                                var cSquare = r * r + q * q + r * q;
                                if (bSquare + cSquare < aSquare)
                                {
                                    var bc = Math.Sqrt(bSquare * cSquare);
                                    if (bSquare + cSquare + bc < aSquare)
                                    {
                                        continue;
                                    }
                                }
                                sum = p + q + r;
                                if (sum < limit)
                                {
                                    validSums.Add(sum);
                                    Console.WriteLine($"{p} {q} {r} {sum}");
                                }
                            }
                        }
                    }
                }
            }

            foreach (var sum in validSums)
            {
                answer += sum;
            }
            return(answer);
        }
Exemple #14
0
        private static bool IsHarshad(long n)
        {
            var digitSum = UtilityFunctions.DigitSum(n);

            return(n % digitSum == 0);
        }
 public static double Solution()
 {
     return(7 * (1 - UtilityFunctions.Choose(60, 20) / (double)UtilityFunctions.Choose(70, 20)));
     //return ExpectedBalls(0,0);
 }