From 287fd1d84a81371fb1ad64de2450762c98be7fc3 Mon Sep 17 00:00:00 2001 From: Jude Southworth Date: Fri, 2 Oct 2020 17:27:19 +0000 Subject: [PATCH] Create problem27.rs --- problem27.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 problem27.rs diff --git a/problem27.rs b/problem27.rs new file mode 100644 index 0000000..f3efa9d --- /dev/null +++ b/problem27.rs @@ -0,0 +1,67 @@ +#[derive(Debug)] +struct MaxValues { + pub a: isize, + pub b: isize, + pub n: isize, +} + +#[derive(Clone, Copy)] +enum IsPrime { + Prime, + NotPrime, +} + +impl IsPrime { + pub fn is_prime(&self) -> bool { + match self { + Self::Prime => true, + + Self::NotPrime => false, + } + } +} + +fn is_number_prime(i: isize, sieve: &Vec) -> bool { + if i < 0 { + false + } + else { + sieve[i as usize].is_prime() + } +} + +fn main() { + let mut sieve = vec![IsPrime::Prime; 1_000_000]; + + for i in 2..999_999_usize { + if is_number_prime(i as isize, &sieve) { + let mut i_m = i * 2; + + while i_m <= 999_999 { + sieve[i_m] = IsPrime::NotPrime; + + i_m += i; + } + } + } + + let mut maxes = MaxValues { a: 0, b: 0, n: 0 }; + + for a in -999..999 { + for b in -1000..1000 { + let mut n: isize = 0; + + while is_number_prime(n.pow(2) + a*n + b, &sieve) { + n += 1; + } + + if n > maxes.n { + maxes.n = n; + maxes.a = a; + maxes.b = b; + } + } + } + + println!("{:?}", maxes); +}