public void SetData()
        {
            primes  = new Primes((long)Math.Pow(maxSize, 2));
            triples = new HashSet <PythagoreanTriple>(new PythComp());
            // https://www.chilimath.com/lessons/geometry-lessons/generating-pythagorean-triples/
            long a, b;

            // Generate all triples where short leg < 100, and long leg (which will become sum of other two legs) < 2*MaxSize
            for (long n = 1; 2 * n <= (maxSize * 2); n++)
            {
                for (long m = n + 1; Math.Pow(m, 2) - Math.Pow(n, 2) <= maxSize; m++)
                {
                    a = (int)(Math.Pow(m, 2) - Math.Pow(n, 2));
                    b = 2 * n * m;
                    if (a > (maxSize * 2) || b > (maxSize * 2))
                    {
                        break;
                    }
                    if (primes.Reduce(ref a, ref b))
                    {
                        continue;
                    }

                    if (!triples.Add(new PythagoreanTriple(a, b)))
                    {
                        throw new Exception("Attempted to add duplicate.");
                    }
                    ;
                }
            }
        }
示例#2
0
        public override long Solve()
        {
            int  longestNumberOfAddends = 0;
            long primeWithLongestSumOfConsecutivePrimes = 0;
            var  primes   = Primes.CalculatePrimesBelow(primeBelow);
            var  maxPrime = primes.Last();

            for (int primeIndex = 0; primeIndex < primes.Count; primeIndex++)
            {
                int  numberOfAddends = 0;
                long sum             = 0;
                for (int i = primeIndex; i < primes.Count; i++)
                {
                    sum += primes.ElementAt(i);
                    numberOfAddends++;
                    if (primes.Contains(sum) && numberOfAddends > longestNumberOfAddends)
                    {
                        longestNumberOfAddends = numberOfAddends;
                        primeWithLongestSumOfConsecutivePrimes = sum;
                    }

                    if (sum > maxPrime)
                    {
                        break;
                    }
                }
            }
            return(primeWithLongestSumOfConsecutivePrimes);
        }
        public void SetData()
        {
            primes        = new Primes(N);
            maxPrimeIndex = primes.lstPrimes.Count;
            factorials    = new int[N + 1][];

            factorials[0] = new int[0];
            factorials[1] = new int[0];

            int[] newVals;

            for (int i = 2; i <= N; i++)
            {
                newVals = factorials[i - 1].ToArray();
                primes.PrimeFactorization_ArrIdx(i, ref newVals, N_MaxEvaluate);
                factorials[i] = newVals;
            }

            int[] divisor = new int[3];
            primes.PrimeFactorization_ArrIdx(multiple, ref divisor);

            nFactorialMinusDivisor = new int[maxPrimeIndex];
            for (int i = 0; i < maxPrimeIndex; i++)
            {
                nFactorialMinusDivisor[i] = factorials[N].GV(i) - divisor.GV(i);
            }

            //Console.WriteLine(maxFactorialDivisor(nFactorialMinusDivisor));
        }
示例#4
0
        public override long Solve()
        {
            var bRange = Primes.CalculatePrimesBelow(MAX_COEFF);
            var aRange = Enumerable.Range(-999, 1999);

            long maxPrimesCount = 0;
            long maxACoeff      = 0;
            long maxBCoeff      = 0;

            foreach (var b in bRange)
            {
                foreach (var a in aRange)
                {
                    long n = 0;
                    while (Primes.IsPrime((n * n) + (a * n) + b))
                    {
                        n++;
                    }
                    if (n > maxPrimesCount)
                    {
                        maxPrimesCount = n;
                        maxACoeff      = a;
                        maxBCoeff      = b;
                    }
                }
            }
            return(maxACoeff * maxBCoeff);
        }
示例#5
0
        public override long Solve()
        {
            var primesBelow   = 10000;
            var primes        = Primes.CalculatePrimesBelow(primesBelow);
            var oddComposites = BuildOddComposites(primes);

            foreach (var oddComposite in oddComposites)
            {
                var found = false;
                for (int i = 0; i < primes.Count && primes.ElementAt(i) < oddComposite && !found; i++)
                {
                    int numberToBeSquared = 1;
                    while (oddComposite > Math.Abs((numberToBeSquared * numberToBeSquared * 2) - primes.ElementAt(i)))
                    {
                        if (oddComposite == primes.ElementAt(i) + (2 * numberToBeSquared * numberToBeSquared))
                        {
                            found = true;
                        }
                        numberToBeSquared++;
                    }
                }
                if (!found)
                {
                    return(oddComposite);
                }
            }
            throw new Exception("Could not find an odd composite below " + primesBelow);
        }
示例#6
0
        public static HashSet <long> CalculateDistinctPrimeFactorsSLOW(int number)
        {
            var primes       = Primes.CalculatePrimesBelow(number + 1);
            var primeFactors = new HashSet <long>();

            return(PrimeFactors(number, primes, primeFactors));
        }
        public void SetData()
        {
            // Get Primes to be used
            maxPrimeEvaluate = (int)Math.Ceiling((double)N / 3);
            primes           = new Primes(maxPrimeEvaluate);
            maxPrimeIndex    = primes.lstPrimes.Count - 1;

            // Set factorization of our divisor D
            d = new int[3]; // Only prime factors are 2 and 5
            primes.PrimeFactorization_ArrIdx(multiple, ref d);

            // Create factorial array
            factorials    = new int[N + 1][];
            factorials[0] = new int[] {};
            factorials[1] = new int[] {};

            int[] newVals;
            for (int i = 2; i <= N; i++)
            {
                newVals = factorials[i - 1].ToArray();
                primes.PrimeFactorization_ArrIdx(i, ref newVals, maxPrimeEvaluate);
                factorials[i] = newVals;
            }

            d = new int[3];
            primes.PrimeFactorization_ArrIdx(multiple, ref d, maxPrimeEvaluate);

            var fact200k = factorials[N];
        }
        public static long Solve()
        {
            int    seed   = 10001;
            Primes primes = new Primes();

            primes.GenerateNPrimes(seed);
            return(primes.list[seed - 1]);
        }
        public static long Solve()
        {
            Primes primes = new Primes();

            primes.CheckNForPrimes(seed);
            //Formatting.PrintList(primes.list);
            answer = primes.list.Sum();
            return(answer);
        }
示例#10
0
        internal static string Solve()
        {
            Timer timer = new Timer();

            timer.Begin();
            long        input   = 600851475143;   //13195;//600851475143;
            Primes      primes  = new Primes();
            List <long> factors = primes.GetPrimeFactors(input);

            return(factors.Last().ToString());
        }
示例#11
0
        public void SetData()
        {
            C              = (reflections + 3) / 2;
            primes         = new Primes((long)Math.Ceiling(Math.Max((double)Math.Sqrt(C), 10000D)));
            cFactorization = primes.PrimeFactorization(C);

            eMax = EMax();
            eMin = EMin(eMax);

            // We'll start considering all values, then subtract off invalid values.
            count = ((eMax - eMin) / 3) + 1;

            evaluatedFactors = new HashSet <long>();
        }
示例#12
0
 public override long Solve()
 {
     for (int i = 9; i > 0; i--)
     {
         var pandigitals = Pandigitals.Generate(i).OrderByDescending(p => p);
         foreach (var pandigital in pandigitals)
         {
             if (Primes.IsPrime(pandigital))
             {
                 return(pandigital);
             }
         }
     }
     return(0);
 }
示例#13
0
        public static long LargestPrimeFactor(long number)
        {
            //start at sqrt of number.
            var  limit = (long)Math.Sqrt(number);
            long largestPrimeFactor = 0;

            for (long i = limit; i > 0; i--)
            {
                if (number % i == 0 && Primes.IsPrime(i))
                {
                    largestPrimeFactor = i;
                    break;
                }
            }
            return(largestPrimeFactor);
        }
        public void SetData()
        {
            primes = new Primes(N);

            factorials    = new SortedDictionary <int, int> [N + 1];
            factorials[0] = new SortedDictionary <int, int> {
            };
            factorials[1] = new SortedDictionary <int, int> {
            };

            for (int i = 2; i <= N; i++)
            {
                factorials[i] = Multiply(factorials[i - 1], i);
            }

            divisor = primes.PrimeFactorization_SD(multiple);
        }
        public void SetData()
        {
            primes = new Primes(sides);

            for (int y = 1; y <= sides / 8; y++)
            {
                for (int x = y + 1; x <= sides / 8; x++)
                {
                    if (primes.IsReducable(x, y))
                    {
                        continue;
                    }
                    segments.Add(new Segment(x, y));
                }
            }

            minArea = 10000000000D;
        }
示例#16
0
        public override long Solve()
        {
            long circularPrimeCount = 0;
            var  primesLookup       = Primes.CalculatePrimesBelow(max);

            foreach (var prime in primesLookup)
            {
                if (PossibleCircularPrime(prime))
                {
                    var rotations = Permutations.GenerateNumberRotations(prime);
                    if (rotations.All(p => primesLookup.Contains(p)))
                    {
                        circularPrimeCount++;
                    }
                }
            }
            return(circularPrimeCount);
        }
示例#17
0
        public void SetData()
        {
            // Generate all of the primes we need
            Primes prm = new Primes(100000);

            // Starting point for value
            minQuantity = (int)Math.Ceiling(Math.Log(divisors, 2));

            // Just use the primes that we need
            prms = prm.lstPrimes.GetRange(0, minQuantity).ToArray();

            // as a reasonable initial value
            int[] min = new int[minQuantity];
            for (int i = 0; i < minQuantity; i++)
            {
                min[i] = 1;
            }
            minValue = GetNumericValue(min);
        }
        public void SetData()
        {
            primes = new Primes(sides);
            List <Segment> segments = new List <Segment>();

            segments.Add(new Segment(1, 1));

            for (int y = 1; y <= sides / 8; y++)
            {
                for (int x = y + 1; x <= sides / 8; x++)
                {
                    if (primes.IsReducable(x, y))
                    {
                        continue;
                    }
                    segments.Add(new Segment(x, y));
                    segments.Add(new Segment(y, x));
                }
            }

            segments.Sort(new SegmentSort_Length());

            // Build base segments
            int j = 0;

            baseSegments = new Segment[baseSegmentCount];
            while (j < baseSegmentCount)
            {
                baseSegments[j] = new Segment(segments[j].points.Item1, segments[j].points.Item2);
                j++;
            }

            // build auxillary segments
            var auxSegList = new List <Segment>();

            while (segments[j].length < segments[(sides / 4)].length * maxSegmentToConsider)
            {
                auxSegList.Add(new Segment(segments[j].points.Item1, segments[j].points.Item2));
                j++;
            }
            auxillarySegments = auxSegList.ToArray();
        }
        public void SetData()
        {
            primes        = new Primes(rows);
            maxPrimeIndex = primes.lstPrimes.Count - 1;

            factorials    = new int[rows + 1][];
            factorials[0] = new int[] {};
            factorials[1] = new int[] {};

            int[] newVals;
            for (int i = 2; i <= rows; i++)
            {
                newVals = factorials[i - 1].ToArray();
                primes.PrimeFactorization_ArrIdx(i, ref newVals);
                factorials[i] = newVals;
            }

            added = new HashSet <long>();
            added.Add(1);
        }
示例#20
0
        public void SetData()
        {
            primes  = new Primes(1000);
            factors = new int[] { 2, 5 };

            factorials    = new int[N + 1][];
            factorials[0] = new int[] { 0, 0 };
            factorials[1] = new int[] { 0, 0 };

            for (int i = 2; i <= N; i++)
            {
                factorials[i] = new int[factors.Length];
                for (int j = 0; j < factors.Length; j++)
                {
                    factorials[i][j] = CountFactorialFactorization(i, factors[j]);
                }
            }

            d = new int[factors.Length];
            primes.PrimeFactorization_ArrIdx(multiple, ref d);
            d = new int[] { d[0], d[2] };
        }
示例#21
0
        public override long Solve()
        {
            int currentCornerNumber = 1;
            int sideLength          = 1;
            int spiral = 1;

            while (RatioOfPrimes >= 0.1M || numberOfPrimes == 0)
            {
                for (int i = 1; i <= 4; i++)
                {
                    var primetoCheck = currentCornerNumber += 2 * spiral;
                    if (Primes.IsPrime(primetoCheck))
                    {
                        numberOfPrimes++;
                    }
                    numberOfCornerNumbers++;
                }
                sideLength += 2;
                spiral++;
            }
            return(sideLength);
        }
示例#22
0
        public override long Solve()
        {
            for (int number = 1490; number < 9999; number++)
            {
                var perms     = Permutations.Generate(number).Where(p => p.ToString().Length == 4);
                var permsList = new List <long>();

                perms.ToList().ForEach(p => { if (Primes.IsPrime(p))
                                              {
                                                  permsList.Add(p);
                                              }
                                       });
                if (permsList.Count >= 4)
                {
                    var sequence = FindSequence(permsList);
                    if (sequence.Count > 0 && !sequence.Contains(1487))
                    {
                        return(long.Parse(sequence[0].ToString() + sequence[1].ToString() + sequence[2].ToString()));
                    }
                }
            }
            throw new Exception("Cannot find sequence....you messed it up");
        }
示例#23
0
        //http://en.wikipedia.org/wiki/Truncatable_prime

        public override long Solve()
        {
            long sum    = 0;
            var  primes = Primes.CalculatePrimesBelow(1000000);

            foreach (var prime in primes)
            {
                bool truncatable = true;
                if (PossibleTruncatablePrime(prime))
                {
                    var leftToRightTruncate = new Truncate(prime, TruncateMethod.LeftToRight);
                    while (leftToRightTruncate.CanTruncate())
                    {
                        if (!primes.Contains(leftToRightTruncate.NextValue()))
                        {
                            truncatable = false;
                            break;
                        }
                    }

                    var rightToLeftTruncate = new Truncate(prime, TruncateMethod.RightToLeft);
                    while (rightToLeftTruncate.CanTruncate())
                    {
                        if (!primes.Contains(rightToLeftTruncate.NextValue()))
                        {
                            truncatable = false;
                            break;
                        }
                    }
                    if (truncatable)
                    {
                        sum += prime;
                    }
                }
            }
            return(sum);
        }
示例#24
0
        //12.5 seconds without filtering out invalid pandigitals.
        //filtering inputs:
        //Filter out pandigitals where d6 is not divisble by 5: 11.4 seconds
        public override long Solve()
        {
            var primes      = Primes.CalculatePrimesBelow(18).ToArray();
            var pandigitals = Pandigitals.GenerateZeroToN(9);

            long sum = 0;

            foreach (var pandigital in pandigitals)
            {
                if (PossibleSubstringDivisibleByPrime(pandigital))
                {
                    var divisibleByPrime = true;
                    for (int i = 0; i < primes.Length && divisibleByPrime; i++)
                    {
                        divisibleByPrime = SubstringDivisibleBy(pandigital, i + 1, primes[i]);
                    }
                    if (divisibleByPrime)
                    {
                        sum += pandigital;
                    }
                }
            }
            return(sum);
        }
示例#25
0
 public Problem51(int primeFamily)
 {
     this.primeFamily = primeFamily;
     primes           = Primes.CalculatePrimesBelow(999999);
 }
示例#26
0
        public static void solve()
        {
            //Console.WriteLine(squaresMax + ":" + cubesMax + ":" + fourthsMax);
            int count = 0;

            int[] primes = Primes.getPrimes(squaresMax);
            int[] nums   = new int[MAX];

            int[] squares = new int[squaresMax];
            int[] cubes   = new int[cubesMax];
            int[] fourths = new int[fourthsMax];

            for (int i = 0; i < primes.Length; i++)
            {
                if (primes[i] != -1 && primes[i] != 0)
                {
                    int num = primes[i];

                    if (i < squaresMax)
                    {
                        squares[i] = num * num;
                    }

                    if (i < cubesMax)
                    {
                        cubes[i] = num * num * num;
                    }

                    if (i < fourthsMax)
                    {
                        fourths[i] = num * num * num * num;
                    }
                }
            }

            foreach (int s in squares)
            {
                if (s == 0)
                {
                    continue;
                }

                foreach (int c in cubes)
                {
                    if (c == 0)
                    {
                        continue;
                    }

                    foreach (int f in fourths)
                    {
                        if (f == 0)
                        {
                            continue;
                        }

                        if (s + c + f < MAX)
                        {
                            if (nums[s + c + f] == 0)
                            {
                                count++;
                                nums[s + c + f] = -1;
                            }
                        }
                    }
                }
            }

            Console.WriteLine(count);

//			for(int i = 0; i < primes.Length; i++){
//				if(primes[i] == -1){
//					continue;
//				}
//				Console.Write(i + "   ");
//				if(i < squaresMax){
//					Console.Write(squares[i] + "   ");
//				}
//
//				if(i < cubesMax){
//					Console.Write(cubes[i] + "   ");
//				}
//
//				if(i < fourthsMax){
//					Console.Write(fourths[i] + "   ");
//				}
//
//				Console.WriteLine();
//			}
        }
示例#27
0
 public Problem26(int maxDenominator)
 {
     this.maxDenominator = maxDenominator;
     primes = Primes.CalculatePrimesBelow(maxDenominator);
 }
示例#28
0
 public void SetData()
 {
     prm = new Primes(1000000);
 }
示例#29
0
 public override long Solve()
 {
     return(Primes.CalculateNthPrime(whichPrime));
 }
 public void SetData()
 {
     primes = new Primes(maxVal);
 }