use crate::Solution;
use pmath::primes::{is_prime, sieve_of_eratosthenes};
problem!(Problem0027, 27, "Quadratic Primes");
impl Solution for Problem0027 {
fn solve(&self) -> String {
let primes_list = sieve_of_eratosthenes(2000);
let mut max_consecutive = 0;
let mut max_product = 0;
for b in &primes_list {
let b = *b as i64;
if b > 1000 {
break;
}
for prime in &primes_list {
let a = *prime as i64 - b - 1;
if a >= 1000 {
break;
}
let mut consecutive_primes = 0;
for n in 0.. {
let polynomial = (n * n) + (a * n) + b;
if polynomial < 2 {
break;
}
if is_prime(polynomial as u64).0 {
consecutive_primes += 1;
} else {
break;
}
}
if consecutive_primes > max_consecutive {
max_consecutive = consecutive_primes;
max_product = a * b;
}
}
}
max_product.to_string()
}
}