public string Run() { foreach (int prime in sieve.GetPrimes(1487)) { int prime2 = prime + STEP; int prime3 = prime + 2 * STEP; if (sieve.IsPrime(prime2) && sieve.IsPrime(prime3)) { List <int> digits1 = GetDigits(prime).ToList(); digits1.Sort(); List <int> digits2 = GetDigits(prime2).ToList(); digits2.Sort(); List <int> digits3 = GetDigits(prime3).ToList(); digits3.Sort(); bool isDesiredNumber = true; for (int i = 0; i < digits1.Count; ++i) { if (digits1[i] != digits2[i] || digits2[i] != digits3[i] || digits1[i] != digits3[i]) { isDesiredNumber = false; break; } } if (isDesiredNumber) { return(prime.ToString() + prime2.ToString() + prime3.ToString()); } } } return(string.Empty); }
public int Run() { int count = 0; int sum = 0; while (count < 11) { int suggestedPrime = searchList[0]; searchList.RemoveAt(0); if (_sieve.IsPrime(suggestedPrime)) { int order = 1; // check if it is right truncatable prime int primeCopy = suggestedPrime; bool isTrancatablePrime = true; while (primeCopy > 0) { if (!_sieve.IsPrime(primeCopy)) { isTrancatablePrime = false; } primeCopy /= 10; order *= 10; } if (isTrancatablePrime && suggestedPrime > 10) { sum += suggestedPrime; count++; } // generate suggested primes by adding from the left for (int i = 0; i < appends.Length; ++i) { searchList.Add(order * appends[i] + suggestedPrime); } } } return(sum); }
/*public void CheckPermutationPrime(int prime) * { * int[] digits = GetDigits(prime).ToArray(); * List<int> perm = new List<int>(); * foreach (int permutation in GetPermutations(digits)) * { * if (!sieve.IsPrime(permutation)) return; * * _checkedPrimes.Add(permutation); * perm.Add(permutation); * } * perm.ForEach(circularPrime => _circularPrimes.Add(circularPrime)); * }*/ public void CheckCircularPrime(int prime) { int length = prime.ToString().Length; List <int> shifts = new List <int>(); int count = 1; _checkedPrimes.Add(prime); shifts.Add(prime); while (count++ < length) { prime = prime / 10 + (prime % 10) * (int)Math.Pow(10, length - 1); if (!sieve.IsPrime(prime)) { return; } _checkedPrimes.Add(prime); shifts.Add(prime); } shifts.ForEach(circularPrime => _circularPrimes.Add(circularPrime)); }
public int Run(int maxA, int maxB) { ESieve sieve = new ESieve(87400); int maxN = -1, coeffProduct = -1; for (int a = -1000; a <= maxA; ++a) { for (int b = -1000; b <= maxB; ++b) { int n = 0; while (sieve.IsPrime(n * n + a * n + b)) { n++; } if (n > maxN) { maxN = n; coeffProduct = a * b; } } } return(coeffProduct); }