http://projecteuler.net/problem=27
Considering quadratics of the form:
n² + an + b, where |a| < 1000 and |b| < 1000 where |n| is the modulus/absolute value of n e.g. |11| = 11 and |-4| = 4 Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.
static void Main(string[] args) { //n² + an + b int maxprimes = 0; int maxproduct = 0; for (int a = 0; a < 1000; a++) { for (int b = 0; b < 1000; b++) { int prime = countPrime(a, b); if (maxprimes < prime) { maxprimes = prime; maxproduct= (-a*b ); } } } Console.WriteLine(maxproduct); } public static int countPrime(int a, int b) { int count = 0; int n = 0; while (true) { double result = Math.Pow(n, 2.00) - (a * n) + b; if (isPrime((int)result) && result >= 0) count++; else return count; n++; } } public static bool isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i < n; ++i) { if ((n % i) == 0) return false; } return true; } |