public static (int, int) GetCoefficientsForQuadraticExpressionWhichProducesMaximumNumberOfPrimes(int maxModulusOfCoefficients) { var longestSequenceOfPrimes = 0; var coefficientsForLongestSequence = (0, 0); foreach (int a in Enumerable.Range(-maxModulusOfCoefficients + 1, 2 * maxModulusOfCoefficients - 2)) { foreach (int b in Enumerable.Range(1, maxModulusOfCoefficients)) { var quadraticFormula = GetQuadraticFormule(a, b); var n = 0; while (PrimeHelper.IsPrime(quadraticFormula(n))) { n++; } if (n > longestSequenceOfPrimes) { longestSequenceOfPrimes = n; coefficientsForLongestSequence = (a, b); } } } return(coefficientsForLongestSequence); }
public static long GetLengthOfNumberSpiralForWhichPercentageOfPrimesOnDiagonalsIsLowerThanLimit(int desiredUpperBoundPercentagePrimes) { var numberOfDiagonalNumbers = 5; var numberOfPrimesOnDiagonals = 3; int increment = 2; int currentPosition = 9; while (!(numberOfPrimesOnDiagonals * 100 <= numberOfDiagonalNumbers * desiredUpperBoundPercentagePrimes)) { increment += 2; // The odd squares lie on the bottom right diagonal, so these cannot be prime. for (int i = 0; i < 3; i++) { currentPosition += increment; if (PrimeHelper.IsPrime(currentPosition)) { numberOfPrimesOnDiagonals++; } } currentPosition += increment; numberOfDiagonalNumbers += 4; } return(increment + 1); }
private static List <long> GetPandigitalPrimes(List <char> characterAllowedTouse) { return(GetPandigitalNumbers(characterAllowedTouse) .Select(number => long.Parse(number)).Where(number => PrimeHelper.IsPrime(number)).ToList()); }