Exemplo n.º 1
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes();

            int i = 9;

            while (true)
            {
                bool foundOddComposite = false;
                if (Primes.IsPrime(i))
                {
                    i += 2; continue;
                }
                long sqrt = (long)Math.Sqrt(i);
                for (int j = 1; j < sqrt; j++)
                {
                    long diff = i - 2 * j * j;
                    if (Primes.IsPrime(diff))
                    {
                        foundOddComposite = true;
                        break;
                    }
                }
                if (!foundOddComposite)
                {
                    return(i);
                }
                i += 2;
            }
        }
Exemplo n.º 2
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes(500);
            List <string> pandigitals = Permutations.Generate("9876543210").ToList();
            List <string> pandigitalsIndexProperty = new List <string>();
            bool          notIndexProperty         = false;

            foreach (var pandigital in pandigitals)
            {
                notIndexProperty = false;
                for (int i = 0; i < 7; i++)
                {
                    if (Int64.Parse(pandigital.Substring(i + 1, 3)) % Primes.AllPrimes[i] != 0)
                    {
                        notIndexProperty = true;
                        break;
                    }
                }
                if (!notIndexProperty)
                {
                    pandigitalsIndexProperty.Add(pandigital);
                }
            }
            var bigIntList = pandigitalsIndexProperty.Select(item => new BigInt(item)).ToList();

            return(new BigInt("").SumBigIntNumbers(bigIntList).Value);
        }
Exemplo n.º 3
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            int replacePrimeFamilyCount = (int)input;

            Primes.InitPrimes();
            Primes.TrimPrimes(9, 1000000);
            var primeList       = Primes.AllPrimes.OrderBy(p => p);
            int primeInitLength = 0;

            foreach (var start in primeList)
            {
                var stringStart       = start.ToString();
                var stringStartLength = stringStart.Length;
                if (stringStartLength > primeInitLength)
                {
                    Primes.InitPrimes((long)Math.Pow(10, stringStartLength));
                    Primes.TrimPrimes((long)(Math.Pow(10, stringStartLength - 1) - 1), (long)Math.Pow(10, stringStartLength));
                    primeInitLength = stringStartLength;
                }
                for (int indecesToReplace = 1; indecesToReplace < stringStartLength; indecesToReplace++)
                {
                    var indeces = Combinations.ChooseStringIndeces(indecesToReplace, stringStartLength);
                    foreach (var index in indeces)
                    {
                        var permutations = Permutations.GenerateReplacements(stringStart, index);
                        if (permutations.Count() >= replacePrimeFamilyCount && permutations.Count(Primes.IsPrime) >= replacePrimeFamilyCount)
                        {
                            return(permutations.Min());
                        }
                    }
                }
            }
            return(0);
        }
Exemplo n.º 4
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            int runAndNumberDistinctPrimeFactors = (int)input;

            Primes.InitPrimes();

            int i        = 2;
            int startRun = 0;
            int runCount = 0;

            while (true)
            {
                if (Primes.UniquePrimeFactors(i, false).Count() == runAndNumberDistinctPrimeFactors)
                {
                    if (runCount == 0)
                    {
                        startRun = i;
                    }
                    runCount++;
                }
                else
                {
                    startRun = 0;
                    runCount = 0;
                }

                if (runCount == runAndNumberDistinctPrimeFactors)
                {
                    return(startRun);
                }
                i++;
            }
        }
Exemplo n.º 5
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes((long)input);
            var primes = Primes.AllPrimes.Where(p => {
                var s = p.ToString();
                return(!s.Contains("0") && !s.Contains("2") && !s.Contains("4") &&
                       !s.Contains("6") && !s.Contains("8"));
            }).ToList();

            primes.Insert(0, 2);

            foreach (long prime in primes)
            {
                if (CircularPrimes.Contains(prime))
                {
                    continue;
                }
                var permutations = Permutations.GenerateRotations(prime).Distinct();
                var addPrime     = permutations.All(Primes.IsPrime);
                if (addPrime)
                {
                    CircularPrimes.AddRange(permutations);
                }
            }
            return(CircularPrimes.Count);
        }
Exemplo n.º 6
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes((long)Math.Sqrt(987654321));
            List <long> permutations = Permutations.Generate(123456789).OrderByDescending(item => item).ToList();

            foreach (long permutation in permutations)
            {
                if (Primes.AllPrimes.All(item => (permutation != item && permutation % item != 0) || permutation == item))
                {
                    return(permutation);
                }
            }
            permutations = Permutations.Generate(12345678).OrderByDescending(item => item).ToList();

            foreach (long permutation in permutations)
            {
                if (Primes.AllPrimes.All(item => (permutation != item && permutation % item != 0) || permutation == item))
                {
                    return(permutation);
                }
            }

            permutations = Permutations.Generate(1234567).OrderByDescending(item => item).ToList();
            foreach (long permutation in permutations)
            {
                if (Primes.AllPrimes.All(item => (permutation != item && permutation % item != 0) || permutation == item))
                {
                    return(permutation);
                }
            }
            return(1);
        }
Exemplo n.º 7
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            var signs = new List <int> {
                1, -1
            };
            var max = (int)input;

            Primes.InitPrimes(max);
            var bPrimes    = Primes.AllPrimes;
            var maxIndex   = 0;
            var maxProduct = new QuadraticCoefficients(0, 0);

            Primes.InitPrimes(80 * 80 + 80 * 1000 + 1000);
            var SquareHash = new Dictionary <int, long>();

            for (int i = 0; i <= max; i++)
            {
                SquareHash.Add(i, (long)Math.Pow(i, 2));
            }

            for (int a = -1 * max + 1; a < max; a += 2)
            {
                foreach (var prime in bPrimes)
                {
                    foreach (var sign in signs)
                    {
                        var  b = prime * sign;
                        bool isPrime;
                        var  tempIndex = 0;
                        do
                        {
                            var quad = SquareHash[tempIndex] + tempIndex * a + b;
                            tempIndex++;
                            isPrime = Primes.IsPrime(quad);
                        } while (isPrime);

                        if (tempIndex > maxIndex)
                        {
                            maxIndex   = tempIndex;
                            maxProduct = new QuadraticCoefficients(a, (int)b);
                        }
                    }
                }
            }
            if (Logging)
            {
                Console.WriteLine(String.Format("{0}:{1}", maxProduct.A, maxProduct.B));
            }
            return(maxProduct.A * maxProduct.B);
        }
Exemplo n.º 8
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes(10000);
            string givenSolution   = input.ToString();
            var    fourDigitPrimes = Primes.AllPrimes.Where(item => item > 999 && item < 10000).ToList();

            foreach (var prime in fourDigitPrimes)
            {
                var permutationPrimes = Permutations.Generate(prime).Where(Primes.IsPrime).Where(p => p.ToString().Length == 4).Distinct().OrderBy(p => p).ToList();
                int count             = permutationPrimes.Count();
                if (count < 3)
                {
                    continue;
                }

                string concatPrimes = "";
                if (Logging)
                {
                    for (int i = 0; i < count; i++)
                    {
                        concatPrimes += permutationPrimes[i].ToString();
                    }
                    Console.WriteLine(concatPrimes);
                }

                for (int i = 0; i < count - 2; i++)
                {
                    for (int j = i + 1; j < count - 1; j++)
                    {
                        var diff = permutationPrimes[j] - permutationPrimes[i];
                        concatPrimes = permutationPrimes[i].ToString() + permutationPrimes[j].ToString();
                        for (int k = j + 1; k < count; k++)
                        {
                            if (permutationPrimes[k] - permutationPrimes[j] == diff)
                            {
                                concatPrimes += permutationPrimes[k].ToString();
                                if (givenSolution != concatPrimes)
                                {
                                    Console.WriteLine();
                                    return(concatPrimes);
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine();
            return(givenSolution);
        }
Exemplo n.º 9
0
        private int SumOfAmicableParis(int input, bool Logging)
        {
            Primes.InitPrimes(input);
            for (int i = 2; i < input; i++)
            {
                if (Primes.IsPrime(i))
                {
                    continue;
                }

                int sumFactorsI;
                int sumFactorsAmicableI;
                if (_LongToSumOfProperFactorsHash.ContainsKey(i))
                {
                    sumFactorsI = _LongToSumOfProperFactorsHash[i];
                }
                else
                {
                    sumFactorsI = (int)Factors.SumFactors(i, true);
                    _LongToSumOfProperFactorsHash.Add(i, sumFactorsI);
                }

                if (_LongToSumOfProperFactorsHash.ContainsKey(sumFactorsI))
                {
                    sumFactorsAmicableI = _LongToSumOfProperFactorsHash[sumFactorsI];
                }
                else
                {
                    sumFactorsAmicableI = (int)Factors.SumFactors(sumFactorsI, true);
                    _LongToSumOfProperFactorsHash.Add(sumFactorsI, sumFactorsAmicableI);
                }

                if (i == sumFactorsAmicableI && i != sumFactorsI)
                {
                    if (!_AmicableNumbers.Contains(i))
                    {
                        _AmicableNumbers.Add(i);
                        _AmicableNumbers.Add(sumFactorsI);
                    }
                }
            }
            return(_AmicableNumbers.Sum());
        }
Exemplo n.º 10
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes((long)input);

            var  maxRunCount   = 0;
            long maxPrime      = 0;
            var  orderedPrimes = Primes.AllPrimes.OrderBy(p => p).ToList();

            for (int i = 1; i < orderedPrimes.Count; i++)
            {
                for (int j = 0; j < Math.Min(orderedPrimes.Count, 4); j++)
                {
                    var  runCount = 0;
                    long runSum   = 0;
                    var  k        = j;
                    while (true)
                    {
                        runSum += orderedPrimes[k];
                        runCount++;
                        if (runSum == orderedPrimes[i])
                        {
                            if (runCount > maxRunCount)
                            {
                                maxRunCount = runCount;
                                maxPrime    = runSum;
                            }
                            break;
                        }
                        if (runSum > orderedPrimes[i])
                        {
                            break;
                        }
                        k++;
                    }
                }
            }
            if (Logging)
            {
                Console.WriteLine(maxRunCount);
            }
            return(maxPrime);
        }
Exemplo n.º 11
0
        public override object Run(RunModes runMode, object input, bool Logging)
        {
            Primes.InitPrimes(10000);

            var primeList = Primes.AllPrimes.Where(p => p != 2 && p != 5).ToList();

            var twoTuples   = GetTwoTuplesList(primeList).ToList();
            var threeTuples = GetThreeTuplesList(twoTuples, primeList).ToList();
            var fourTuples  = GetFourTuplesList(threeTuples, primeList).ToList();
            var fiveTuples  = GetFiveTuplesList(fourTuples, primeList);

            if (fiveTuples.Count > 0)
            {
                if (Logging)
                {
                    Console.WriteLine(fiveTuples.First());
                }
                return(fiveTuples.First().Sum);
            }
            return(0);
        }
Exemplo n.º 12
0
 public override object Run(RunModes runMode, object input, bool Logging)
 {
     Primes.InitPrimes(1000000);
     return(Primes.PrimeAtIndex((int)input - 1));
 }